Analysis in R: Manipulating Factor! About Factor
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.
<おすすめのRに関する書籍です>
Rによる統計データ解析 | 小池 祐太, 村田 昇, 吉田 朋広
確率論の基礎から推定・検定・回帰までを、数式とRのコードの両方で追える大学教科書。理論をきちんと押さえたい人向け。
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 zI hope this makes your analysis a little easier !!
スポンサーリンク