首页 > 代码库 > R因子

R因子

factor(x = character(), levels, labels = levels,

       exclude = NA, ordered = is.ordered(x), nmax = NA)

 

levels因子水平,如果不指定,就是x中所有不重复的值

labels水平标识名称

exclude排除哪些水平

ordered—TRUE表示有序因子,FALSE表示无序因子

nmax水平个数上限

 

> f<-c("Spark","RDD","Scala","MLlib","GraghX", "Spark","Scala","GraghX","Spark","Scala")

> f1<-factor(f)

> class(f1)

[1] "factor"

> str(f1)

 Factor w/ 5 levels "GraghX","MLlib",..: 5 3 4 2 1 5 4 1 5 4

> length(f1)  数据长度,而不是个数

[1] 10

 

> f2<-factor(f,levels=c("Spark","RDD","Scala"))

> f2

 [1] Spark RDD   Scala <NA>  <NA>  Spark

 [7] Scala <NA>  Spark Scala

Levels: Spark RDD Scala

> f3<-factor(f,levels=c("Spark","RDD","Scala","MLlib","GraghX","Hadoop","Hive"))

> f3

 [1] Spark  RDD    Scala  MLlib  GraghX

 [6] Spark  Scala  GraghX Spark  Scala

7 Levels: Spark RDD Scala ... Hive

 

> fa<-c(1,2)

> fa1<-factor(fa,labels = c("男","女"))

> str(fa1)

 Factor w/ 2 levels "男","女": 1 2

R因子