R基本数据管理(学习笔记)

对于数据的管理,有一些个人的理解,再次申明,这只是我个人的学习笔记,不喜勿喷.

1、变量的重编码:

solution one:

类似于python的列表表达式,其加强版为within(),在使用这种方法的时候,千万要注意语句的执行顺序,这个对最终的结果影响很大

example:

对于women这个dataframe,

data<-women

data<-within(data,
+ {
+ level<-"low"
+ level[height<60]<-"mid"
+ level[height<70]<-"normal"
+ level[height>=70]<-"high"
+ })

这种写法是不行的,原因是:后一个赋值会覆盖掉前面的赋值: level[height<70]<-"normal"会覆盖掉 level[height<60]<-"mid"(小于60的必然会小于70)

这样, level[height<60]<-"mid"就不会起作用。

solution two:

使用car包的recode()函数
solution three:
使用doBy的recodevar()函数
solution four:
使用自带的函数cut()

 

posted @ 2015-07-09 17:02  EIFER  阅读(286)  评论(0编辑  收藏  举报