对于初学者来说,R语言中的factor有些难以理解。如果直译factor为“因子”,使得其更加难以理解。我倾向于不要翻译,就称其为factor,然后从几个例子中理解:

  1. <span style="font-size:12px;">data <- c(1,2,2,3,1,2,3,3,1,2,3,3,1)
  2. data
  3. </span>

显示结果:

  1. <span style="font-size:12px;"> [1] 1 2 2 3 1 2 3 3 1 2 3 3 1</span>

然后运行:

  1. <span style="font-size:12px;">fdata <- factor(data)
  2. fdata </span>

显示结果:

  1. <span style="font-size:12px;"> [1] 1 2 2 3 1 2 3 3 1 2 3 3 1
  2. Levels: 1 2 3</span>

继续查看class

  1. <span style="font-size:12px;">class(fdata)
  2. [1] "factor"
  3. class(data)
  4. [1] "numeric"</span>

可以看到,factor()函数将原来的数值型的向量转化为了factor类型。factor类型的向量中有Levels的概念。Levels就是factor中的所有元素的集合(没有重复)。我们可以发现Levels就是factor中元素排重后且字符化的结果!因为Levels的元素都是character。

  1. <span style="font-size:12px;">levels(fdata)
  2. [1] "1" "2" "3"</span>

我们可以在factor生成时,通过labels向量来指定levels,继续上面的程序:

  1. <span style="font-size:12px;">rdata <- factor(data,labels=c("I","II","III"))
  2. rdata
  3. </span>

显示结果:

  1. <span style="font-size:12px;">[1] I   II  II  III I   II  III III I   II  III III I
  2. Levels: I II III</span>

也可以在factor生成以后通过levels函数来修改:

  1. <span style="font-size:12px;">rdata <- factor(data,labels=c("e","ee","eee"))
  2. rdata
  3. </span>

显示结果:

  1. <span style="font-size:12px;"> [1] e   ee  ee  eee e   ee  eee eee e   ee  eee eee e
  2. Levels: e ee eee</span>

看到这里,我们马上就会意识到,为什么factor要有levels?因为factor是一种更高效的数据存储方式。对于不同的变量,只需要存储一次就可以,具体的数据内容只要存储相应的整数内容就可以了。因此,read.table()函数会默认把读取的数据以factor格式存储,除非你指定类型。

并且,factors可以指定数据的顺序:

  1. <span style="font-size:12px;"> 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")</span><pre tabindex="0" class="GCWXI2KCJKB" id="rstudio_console_output" style="font-family: 'Lucida Console'; font-size: 10pt !important; outline: none; border: none; word-break: break-all; margin: 0px; -webkit-user-select: text; white-space: pre-wrap !important; line-height: 15px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><pre name="code" class="html"><span style="font-size:12px;">mons <- factor(mons)
  2. </span><pre name="code" class="html"><span style="font-size:12px;">table(mons)
  3. </span>

显示结果:

  1. <span style="font-size:12px;">mons
  2. April    August  December  February   January      July     March       May  November
  3. 2         4         1         2         3         1         1         1         5
  4. October September
  5. 1         3 </span>

显然月份是有顺序的,我们可以为factor指定顺序

  1. mons = factor(mons,levels=c("January","February","March","April","May","June","July","August","September","October","November","December"),ordered=TRUE)

现在运行:

  1. table(mons)
  2. mons
  3. January  February     March     April       May      June
  4. 3         2         1         2         1         0
  5. July    August September   October  November  December
  6. 1         4         3         1         5         1

需要注意的是数值型变量与factor的互相转化:

  1. fert = c(10,20,20,50,10,20,10,50,20)
  2. mean(fert)
  3. [1] 23.33333

转化后:

  1. mean(factor(fert))
  2. Warning message:
  3. In mean.default(factor(fert)) : 参数不是数值也不是逻辑值:回覆NA

那我们这里,是不是可以直接用as.numeric() 转化呢?

  1. mean(as.numeric(factor(fert)))
  2. [1] 1.888889

发现上面是错误的!
这里需要这么转回去:

    1. ff <- factor(fert)
    2. mean(as.numeric(levels(ff)[ff]))
    3. [1] 23.33333

R语言中的factor的更多相关文章

  1. 掌握R语言中的apply函数族(转)

    转自:http://blog.fens.me/r-apply/ 前言 刚开始接触R语言时,会听到各种的R语言使用技巧,其中最重要的一条就是不要用循环,效率特别低,要用向量计算代替循环计算. 那么,这是 ...

  2. R语言中apply函数

    前言 刚开始接触R语言时,会听到各种的R语言使用技巧,其中最重要的一条就是不要用循环,效率特别低,要用向量计算代替循环计算. 那么,这是为什么呢?原因在于R的循环操作for和while,都是基于R语言 ...

  3. R语言中的MySQL操作

    R语言中,针对MySQL数据库的操作执行其实也有很多中方式.本人觉得,熟练掌握一种便可,下面主要就个人的学习使用情况,总结其中一种情况-----使用RMySQL操作数据库. 1.下载DBI和RMySQ ...

  4. R语言中的read.table()

    参考资料:http://www.cnblogs.com/xianghang123/archive/2012/06/06/2538274.html read.table(file, header = F ...

  5. R语言中 fitted()和predict()的区别

    fitted是拟合值,predict是预测值.模型是基于给定样本的值建立的,在这些给定样本上做预测就是拟合.在新样本上做预测就是预测. 你可以找一组数据试试,结果如何. fit<-lm(weig ...

  6. R语言中Fisher判别的使用方法

    最近编写了Fisher判别的相关代码时,需要与已有软件比照结果以确定自己代码的正确性,于是找到了安装方便且免费的R.这里把R中进行Fisher判别的方法记录下来. 1. 判别分析与Fisher判别 不 ...

  7. R语言中的Apriori关联规则的使用

    1.下载Matrix和arules包 install.packages(c("Matrix","arules")) 2.载入引入Matrix和arules包 # ...

  8. R 语言中 data table 的相关,内存高效的 增量式 data frame

    面对的是这样一个问题,不断读入一行一行数据,append到data frame上,如果用dataframe,  rbind() ,可以发现数据大的时候效率明显变低. 原因是 每次bind 都是一次重新 ...

  9. rugarch包与R语言中的garch族模型

    来源:http://www.dataguru.cn/article-794-1.html rugarch包是R中用来拟合和检验garch模型的一个包.该包最早在http://rgarch.r-forg ...

随机推荐

  1. php分享二十三:字符编码

    1:ASCII 在计算机中,所有的数据在存储和运算时都要使用二进制数表示(因为计算机用高电平和低电平分别表示1和0),例如,像a.b.c.d这样的52个字母(包括大写).以及0.1等数字还有一些常用的 ...

  2. 腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动

    在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH ...

  3. pc客户端网页录音和压缩

    web录音的功能,也就是怎么使用 getUserMedia 音频上传 栗子中最后返回的是Blob数据 return new Blob([dataview], { type: type }) 因为对ht ...

  4. GGGGCCCC

    Evaluating and improving remembered sets in the HotSpot G1 garbage collector http://www.diva-portal. ...

  5. django_simple_captcha使用笔记

    一.先来官方文档的步骤: Install django-simple-captcha via pip: pip install  django-simple-captcha Add captcha t ...

  6. Presentational and Container Components

    https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0 There’s a simple pattern I fi ...

  7. hive入门

    hive 当前用到的就这些,以后用到的再补充. 参考自官方文档 大小写不敏感 创建/删除数据库 CREATE/DROP DATABASE|SCHEMA [IF NOT EXISTS] <data ...

  8. c语言中条件编译相关的预编译指令

    一. 内容概述 本文主要介绍c语言中条件编译相关的预编译指令,包括#define.#undef.#ifdef.#ifndef.#if.#elif.#else.#endif.defined. 二.条件编 ...

  9. 【Bootstrap Method】Evaluating The Accuracy of a Classifier

    自助法介绍: 非参数统计中一种重要的估计统计量方差进而进行区间估计的统计方法,也称为自助法.其核心思想和基本步骤如下:(1)采用重抽样技术从原始样本中抽取一定数量(自己给定)的样本,此过程允许重复抽样 ...

  10. Ubuntu 16.04 64位安装arm-linux-gcc交叉编译器以及samba服务器

    交叉编译器是嵌入式开发的必要工具,但是由于目前大多数人使用64位ubuntu,在照着很多教程做的时候,就会失败,失败原因是64位ubuntu需要额外安装32位的兼容包.以arm-linux-gcc-3 ...