R中数据帧的列中的级别是多少?
大多数人对R中的级别和角色感到困惑,尤其是新手。不同之处在于,级别专门定义了因子列的因子级别,而字符是简单的字符列,它不是因子,也不用作因子但可以转换为因子。
示例
考虑以下数据帧-
> x1<-factor(sample(LETTERS[1:4],20,replace=TRUE)) > x2<-sample(LETTERS[1:4],20,replace=TRUE) > df1<-data.frame(x1,x2) > df1输出结果
x1 x2 1 B B 2 B A 3 D D 4 D C 5 C A 6 D C 7 A D 8 D B 9 D C 10 B B 11 C B 12 D A 13 C D 14 B B 15 C B 16 C A 17 B A 18 D C 19 C B 20 D D
查看df1的结构以了解factor和character列之间的区别-
> str(df1) 'data.frame': 20 obs. of 2 variables: $ x1: Factor w/ 4 levels "A","B","C","D": 2 2 4 4 3 4 1 4 4 2 ... $ x2: chr "B" "A" "D" "C" ...
示例
> y1<-factor(sample(c("Winter","Spring","Summer"),20,replace=TRUE)) > y2<-rnorm(20) > df2<-data.frame(y1,y2) > df2输出结果
y1 y2 1 Summer -0.9006581 2 Winter 0.8897190 3 Summer 0.2585291 4 Spring 1.5118381 5 Winter -1.0277900 6 Winter 0.1853884 7 Spring 0.1425927 8 Spring -0.1824645 9 Summer 1.6294306 10 Summer 1.3320479 11 Spring -0.1468691 12 Spring 0.7244621 13 Spring -0.4379905 14 Spring 1.0983712 15 Summer -1.0212200 16 Winter 0.5164757 17 Summer 2.2103486 18 Summer 0.6049139 19 Winter -0.1642906 20 Spring 1.5057525
查看df1的结构以了解因子和数值列之间的区别-
> str(df2) 'data.frame': 20 obs. of 2 variables: $ y1: Factor w/ 3 levels "Spring","Summer",..: 2 3 2 1 3 3 1 1 2 2 ... $ y2: num -0.901 0.89 0.259 1.512 -1.028 ...