注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

阿弥陀佛

街树飘影未见尘 潭月潜水了无声 般若观照心空静...

 
 
 

日志

 
 
关于我

一直从事气象预报、服务建模实践应用。 注重气象物理场、实况场、地理信息、本体知识库、分布式气象内容管理系统建立。 对Barnes客观分析, 小波,计算神经网络、信任传播、贝叶斯推理、专家系统、网络本体语言有一定体会。 一直使用Java、Delphi、Prolog、SQL编程。

网易考拉推荐

Factors in R  

2016-11-17 15:44:33|  分类: R |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Factors in R
Factors in R are stored as a vector of integer values with a corresponding set of character values to use when the factor is displayed.

> data = c(1,2,2,3,1,2,3,3,1,2,3,3,1)
> fdata = factor(data)
> fdata
 [1] 1 2 2 3 1 2 3 3 1 2 3 3 1
Levels: 1 2 3
> rdata = factor(data,labels=c("I","II","III"))
> rdata
 [1] I   II  II  III I   II  III III I   II  III III I  
Levels: I II III
> levels(fdata) = c('I','II','III')
> fdata
 [1] I   II  II  III I   II  III III I   II  III III I  
Levels: I II III
###########################################################
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
 [1] March     April     January   November  January   September October 
 [8] September November  August    January   November  November  February
[15] May       August    July      December  August    August    September
[22] November  February  April   
11 Levels: April August December February January July March May ... September
> tmons <- table(mons)
> tmons
mons
    April    August  December  February   January      July     March       May
        2         4         1         2         3         1         1         1
 November   October September
        5         1         3
Although the months clearly have an ordering, this is not reflected in the output of the table function. Additionally, comparison operators are not supported for unordered factors.
#########################################################################################
Creating an ordered factor solves these problems:
> mons = factor(mons,levels=c("January","February","March",
+                "April","May","June","July","August","September",
+                "October","November","December"),ordered=TRUE)
> mons
 [1] March     April     January   November  January   September October 
 [8] September November  August    January   November  November  February
[15] May       August    July      December  August    August    September
[22] November  February  April   
12 Levels: January < February < March < April < May < June < ... < December
> mons[1] < mons[2]
[1] TRUE
> table(mons)
mons
  January  February     March     April       May      June      July    August
        3         2         1         2         1         0         1         4
September   October  November  December
        3         1         5         1
################################################################
逆变换
> fert = c(10,20,20,50,10,20,10,50,20)
> fert = factor(fert,levels=c(10,20,50),ordered=TRUE)
> fert
[1] 10 20 20 50 10 20 10 50 20
Levels: 10 < 20 < 50
> clev <- levels(fert)[fert]
> clev
[1] "10" "20" "20" "50" "10" "20" "10" "50" "20"
> nlev <- as.numeric(clev)
> nlev
[1] 10 20 20 50 10 20 10 50 20
> mean(nlev)
[1] 23.33333
##############################################################
> lets = sample(letters,size=100,replace=TRUE)
> lets
  [1] "c" "x" "q" "t" "s" "c" "u" "l" "z" "p" "n" "g" "k" "i" "g" "o" "i" "y"
 [19] "l" "t" "c" "x" "f" "j" "c" "y" "p" "b" "h" "n" "h" "g" "r" "h" "n" "l"
 [37] "n" "k" "w" "v" "y" "m" "e" "p" "s" "t" "z" "d" "n" "l" "k" "e" "g" "v"
 [55] "v" "a" "t" "z" "i" "w" "p" "h" "p" "o" "v" "n" "z" "r" "d" "i" "i" "o"
 [73] "h" "a" "j" "f" "k" "v" "q" "k" "m" "z" "m" "e" "x" "m" "k" "m" "t" "z"
 [91] "i" "k" "v" "d" "a" "v" "w" "q" "o" "v"
  评论这张
 
阅读(39)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017