R语言中的factor
对于初学者来说,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;">mons
- April 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)
- mons
- January February March April May June
- 3 2 1 2 1 0
- July August September October November December
- 1 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的更多相关文章
- 掌握R语言中的apply函数族(转)
转自:http://blog.fens.me/r-apply/ 前言 刚开始接触R语言时,会听到各种的R语言使用技巧,其中最重要的一条就是不要用循环,效率特别低,要用向量计算代替循环计算. 那么,这是 ...
- R语言中apply函数
前言 刚开始接触R语言时,会听到各种的R语言使用技巧,其中最重要的一条就是不要用循环,效率特别低,要用向量计算代替循环计算. 那么,这是为什么呢?原因在于R的循环操作for和while,都是基于R语言 ...
- R语言中的MySQL操作
R语言中,针对MySQL数据库的操作执行其实也有很多中方式.本人觉得,熟练掌握一种便可,下面主要就个人的学习使用情况,总结其中一种情况-----使用RMySQL操作数据库. 1.下载DBI和RMySQ ...
- R语言中的read.table()
参考资料:http://www.cnblogs.com/xianghang123/archive/2012/06/06/2538274.html read.table(file, header = F ...
- R语言中 fitted()和predict()的区别
fitted是拟合值,predict是预测值.模型是基于给定样本的值建立的,在这些给定样本上做预测就是拟合.在新样本上做预测就是预测. 你可以找一组数据试试,结果如何. fit<-lm(weig ...
- R语言中Fisher判别的使用方法
最近编写了Fisher判别的相关代码时,需要与已有软件比照结果以确定自己代码的正确性,于是找到了安装方便且免费的R.这里把R中进行Fisher判别的方法记录下来. 1. 判别分析与Fisher判别 不 ...
- R语言中的Apriori关联规则的使用
1.下载Matrix和arules包 install.packages(c("Matrix","arules")) 2.载入引入Matrix和arules包 # ...
- R 语言中 data table 的相关,内存高效的 增量式 data frame
面对的是这样一个问题,不断读入一行一行数据,append到data frame上,如果用dataframe, rbind() ,可以发现数据大的时候效率明显变低. 原因是 每次bind 都是一次重新 ...
- rugarch包与R语言中的garch族模型
来源:http://www.dataguru.cn/article-794-1.html rugarch包是R中用来拟合和检验garch模型的一个包.该包最早在http://rgarch.r-forg ...
随机推荐
- [CTCI] 单词最近距离
单词最近距离 题目描述 有一篇文章内含多个单词,现给定两个单词,请设计一个高效算法,找出文中这两个单词的最短距离(即最少相隔的单词数,也就是两个单词在文章中位置的差的绝对值). 给定一个string数 ...
- python 给文件批量加‘“’ ”,"
file = open('face.txt','r') filew = open('face1.txt','w') try: for line in file: print(line) if line ...
- Egret入门了解
0.前言 这个星期没有什么事做,就想找点技术了解一下.前段时间看过Egret,用来开发HTML5小游戏.一开始以为很麻烦的,但是经过这两天了解了一下,如果用这个游戏引擎来开发一些简单的游戏,还是蛮方便 ...
- Fabric V1 交易的生命周期
Fabric v1 Transaction Lifecycle1 Client application creates tran proposeal (chinacode function and a ...
- [SQL in Azure] High Availability and Disaster Recovery for SQL Server in Azure Virtual Machines
http://msdn.microsoft.com/en-us/library/azure/jj870962.aspx Microsoft Azure virtual machines (VMs) w ...
- [SQL Server 2014] 微软将于年底发布新版数据库SQL Server 2014
在今年的TechEd大会上,微软宣布SQL Server 2014的第一个技术预览版.SQL Server 2014的重点包括内存OLTP.实时的大数据分析.支持混合云端,以及提供更完整的商业智能(B ...
- pre 标签的使用注意事项
.news-msg{ // padding: 5px; white-space: pre-wrap; word-wrap: break-word; font-family:'微软雅黑'; } < ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 【PHP爬虫】curl+simple_html_dom 抓取百度最新消息新闻标题,来源,URL
<title>新闻转载统计</title> <script> function submit(){ wd=document.getElementById('name ...
- 详解Android开发中Activity的四种launchMode
Activity栈主要用于管理Activity的切换.当使用Intent跳转至某个目标Activity,需要根据目标Activity的加载模式来加载. Activity一共有以下四种launchMod ...