因子型(factor)表示编号或登记,是用来存储类别的数据类型,是离散的,与连续性值相对。如果把数字作为因子,那么在导入数据后,需要将向量转换为因子(factor),而因子在整个计算过程中不再作为数值,而是作为“符号”。 讲的很好的R因子:https://blog.csdn.net/hsdcc217/article/details/78510087
data <- c(1,2,2,3,1,2,3,3,1,2,3,3,1) data fdata<-factor(data) fdata class(fdata) class(data) levels(fdata) rdata<-factor(data,labels = c("I","II","III")) rdata # rdata<-factor(data.labels=c("e","ee","eee")) comma bug rdata<-factor(data,labels=c("e","ee","eee")) rdata mons <- c("March","April","January","November","January", "September","October","September","November","August", "January","November","November","February","May","August", "July","December","August","August","September","November", "February","April") mons<-factor(mons) mons mons = factor(mons,levels=c("January","February","March","April","May","June","July","August","September","October","November","December"),ordered=TRUE) mons table(mons) ?table()
#factor()函数将原来的数值型的向量转化为factor类型。factor类型的向量有levels的概念。Levels就是factor所有因素的集合(没有重复),Levels就是factor中元素排除重复后字符化的结果,levels元素都是character类型,可以在生成factor时,指定levels。
factor最大的作用是指定数据的顺序,即指定后边作图过程自变量的显示顺序。
Ref2:
因子水平(Level)表示因子的值域。因子的每个元素只能取因子水平中的值或缺失。
创建因子用factor()函数创建因子型变量。factor(x=character(),levels,lables=levels,
还可以通过factor()修改level