Analysis in R: Manipulating Factor! About Factor

RAnalytics
スポンサーリンク

When manipulating data in R, you may create a vector or data frame data whose class is factor. If you are not familiar with the operation, handling factors, called factors, can be tricky. Therefore, we will introduce commands related to factor. If you are familiar with factor, it is very useful, for example, when creating a graph, the order of axes are not interchanged, etc. When you are manipulating data in R and an error occurs, checking with class (data) is a shortcut to solving the problem.

スポンサーリンク

Command

See the command and package help for details.

#Creating Data Examples
Data <- LETTERS[c(5, 9, 12, 1, 4, 9, 22, 4, 5)]

#Factorization of data, where level is a reference sequence
LT <- factor(Data , levels = LETTERS)

#Check
LT
[1] E I L A D I V D E
Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y 

#Check the order of the strings in the sample data
as.integer(LT)
[1]  5  9 12  1  4  9 22  4  5

#Simplified reference sequence display with sample data
factor(LT)
[1] E I L A D I V D E
Levels: A D E I L V

#Delete unused levels
LT[, drop = TRUE]
[1] E I L A D I V D E
Levels: A D E I L V

#Change level
levels(LT) <- letters

#Check
LT
[1] e i l a d i v d e
Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z

I hope this makes your analysis a little easier !!

Amazon audibleの登録の紹介

プライム会員限定で2024年7月22日まで3か月無料体験キャンペーン開催中です。無料体験後は月額1,500円で聞き放題です。なお、聞き放題対象外の本はAudible会員であれば非会員価格の30%引きで購入することが可能です。

Amazon audibleはプロのナレーターが朗読した本をアプリで聞くことができるサービスで、オフライン再生も可能です。通勤や作業のお供にAmazon audibleのご登録はいかがでしょうか。

・AmazonのAudible

https://amzn.to/3L4FI5o

Copied title and URL