本文へスキップ
からだにいいもの

Rのトピックスを中心に『まだ、まだ、知らない、役に立つ情報?』を発信します。

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.

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から提供されたものであり、更新または削除される場合があります。
karada-goodはAmazonアソシエイトとして、適格販売により収入を得ています。