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

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

显示结果:

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

然后运行:

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

显示结果:

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

继续查看class

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

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

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

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

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

显示结果:

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

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

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

显示结果:

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

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

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

<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)
</span><pre name="code" class="html"><span style="font-size:12px;">table(mons)
</span>

显示结果:

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

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

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

现在运行:

table(mons)
monsJanuary  February     March     April       May      June3         2         1         2         1         0July    August September   October  November  December1         4         3         1         5         1

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

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

转化后:

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

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

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

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

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

R语言中的factor相关推荐

  1. r语言显示找不到read_html,R语言中read.table函数不常见的用法-文本中有#注释符号...

    自从开始学习R语言,read.table是较早接触到的函数,因为要读取数据,也因为最开始学习数据分析是用"制表分隔符的txt"文件比较多,xlsm在linux系统中又不太合适,所以 ...

  2. 1071svm函数 r语言_如何利用R语言中的rpart函数建立决策树模型

    决策树是根据若干输入变量的值构造出一个适合的模型,以此来预测输出变量的值,并用树形结构展示出来.决策树主要有两个类别:分类树和回归树.分类树主要针对离散的目标变量,回归树则针对连续的目标变量.R语言中 ...

  3. r语言中c函数错误,R语言中c()函数与paste()函数的区别说明

    c()函数:将括号中的元素连接起来,并不创建向量 paste()函数:连接括号中的元素 例如 c(1, 2:4),结果为1 2 3 4 paste(1, 2:4),结果为"1 2" ...

  4. r语言中的shiny教程_如何使用Shiny在R中编写Web应用程序

    r语言中的shiny教程 新年快乐! 这个月我忙于撰写一些较大的文章,因此请在接下来的几周内查找这些文章. 对于本月的Nooks和Crannies,我想简要指出一个我一直在用它进行自我教育的出色R库. ...

  5. R语言中if语句使用方法之超详细教程

    在R语言中,if属于一种分支结构,即根据某个条件执行相关的语句.R中的if语句与else配合主要有3种结构. 单个if语句 if(cond) {expr} 其它语句 即当括弧中的cond条件为TRUE ...

  6. R语言中GCC编译的问题(续)

    这篇文章承接R语言中GCC编译的问题,这篇文章主要解决我在Linux系统上安装"expm"出现的问题. 出现的问题 这个问题非常的有趣,因为我在两台服务器分别安装同一个包,其中一台 ...

  7. r语言中paste函数_R中的paste()函数-简要指南

    r语言中paste函数 Using the paste() function in R will be straight and simple. In this tutorial let's see ...

  8. r语言中的while循环_R编程中的While循环

    r语言中的while循环 In addition to the for loop we discussed earlier, R also offers another kind of loop to ...

  9. R语言中的igraph包绘制网络图

    本文转自网络. R语言中的igraph包可以很方便地画出网络图,在社交关系分析等领域发挥重要作用,下面介绍包中一个重要的函数graph_from_data_frame(). graph_from_da ...

最新文章

  1. python变量初始化的位置不当、程序结果可能会出现问题_解决tensorflow由于未初始化变量而导致的错误问题...
  2. gitee怎么看用户名_教你手机怎么远程连接云服务器
  3. QT自定义图表上不同元素的外观
  4. 华南理工专科计算机随堂联系,华南理工大学网络教育计算机基础随堂练习第三章...
  5. 13、oracle数据库下的游标
  6. 【洛谷 - P3376 】【模板】网络最大流
  7. 安装fastdfs依赖包
  8. iphone已停用怎么解锁_iPhone多次输错密码已停用,连接iTunes,怎么办?
  9. 固定定位小技巧(HTML、CSS)
  10. java开发微信支付接口_JAVA微信支付接口开发——支付
  11. Python 爬取留言板留言(一):单进程版+selenium模拟
  12. 三星 4521 linux 驱动下载,三星4521f驱动
  13. 转载 GIS的下个十年(Cary Mann, vice president, Bentley)
  14. windows资源管理器对文件右键未响应!电脑小白求救[哭唧唧]!!!
  15. windows7台式计算机网线连接,win7台式机连接wifi的方法步骤详解(2)
  16. lzx: ssh: connect to host lzx port 22: Connection timed out-------hadoop
  17. 舞钢大业投资王恒:央行再度定向降准力挺“三农”小微覆盖大部分的城市商业银行
  18. 立体匹配---立体匹配过程
  19. 给打算找技术工作的毕业生几点建议
  20. 正常正则表达式(不允许为空)

热门文章

  1. 如何在WPS的word中使用 Mathpix Snipping 和 MathType进行快速输入数学公式
  2. 【C语言航路】第十一站:字符串、字符和内存函数
  3. 电影售票系统遇到的问题
  4. 计算机网络笔记----概述
  5. Spring Boot 实现 AI 人脸识别功能,So Easy!
  6. 协众信息技术平面设计的8种类型
  7. mysql deadlock found_mysql死锁 Deadlock found when trying to get lock; try restarting transaction
  8. pygame 飞机大战子弹的编写(二)让子弹动起来
  9. 双人坦克大战 - Unity3D
  10. 自学看视频增强java基础