摘要: 仅用于记录R语言学习过程:
内容提要:
数据排序:sort()函数、rank()函数、order()函数;
长宽型数据的转换:stack()函数、reshape()函数、reshape2扩展包中的函数:melt()函数、dcast()函数
变量的因子化:factor()函数、cut()函数、ifelse()函数、car扩展包中的recode()函数
正文:
数据排序、长宽型数据的转换
n 数据排序
u sort()函数:可对数字和字符串进行排序
x <- sample(1:100,10)
x
[1] 64 38 93 74 22 87 59 30 8 24
sort(x)
[1] 8 22 24 30 38 59 64 74 87 93
sort(x,decreasing = TRUE)
[1] 93 87 74 64 59 38 30 24 22 8
示例二:
y <- c('ab','bc','cde','c')
sort(y)
[1] "ab" "bc" "c" "cde"
sort(y,decreasing = TRUE)
[1] "cde" "c" "bc" "ab"
u rank()函数:秩次(排名):给出数字的位次,如果有两个相同的,取位置的平均数。
示例:
z <- c(1,2,3,3,4,4,5,6,6,6,7,8)
rank(z)
[1] 1.0 2.0 3.5 3.5 5.5 5.5 7.0 9.0 9.0 9.0 11.0
[12] 12.0
u order()函数:最常用。返回的是向量的下标,按照向量从小到大的顺序返回
> x
[1] 78 75 41 72 85 32 77 47 80 51
> order(x)
[1] 6 3 8 10 4 2 7 1 9 5
> x[order(x)]
[1] 32 41 47 51 72 75 77 78 80 85
也可以对数据框进行排序:
head(iris[order(iris$我,iris$是),])
n 长宽型数据的转换
n 长型:堆栈,数据间有不同的分类(如同属一类);
n 宽型:数据内容相对唯一
n stack()函数:(堆栈的意思)
> freshman <- c(12,23,24)
> sophomores <- c(25,36,73)
> juniors <- c(32,46,57)
> data.frame(fr= freshman,so = sophomores,jun = juniors)
fr so jun
1 12 25 32
2 23 36 46
3 24 73 57
> height <- stack(list(fresh =freshman,sopho = sophomores,juni = juniors))
> height
values ind
1 12 fresh
2 23 fresh
3 24 fresh
4 25 sopho
5 36 sopho
6 73 sopho
7 32 juni
8 46 juni
9 57 juni
> tapply(height$values,height$ind,mean) #按照分类求均值,tapply()函数
fresh sopho juni
19.66667 44.66667 45.00000
n reshape()函数:
u 宽型数据:参数设置:reshape(变量名,数值名称,idvar:标识变量,timevar用于接收‘次数’,direction 设置为宽型数据格式)
wide <- reshape(Indometh,v.names = 'conc',idvar = 'Subject',
timevar ='time',direction ="wide")
head(wide)
u 长型数据:reshape(文件名,idvar,varying指拟用于区分出来的内容。)
> long <- reshape(wide,idvar = "subject",varying = list(2:12),
+ v.names = "concentration",direction ="long")
> View(long)
n reshape2扩展包中的函数
u melt()函数:参数设置:data=文件名,id.vars 标识变量
new_iris <- melt(data = iris,id.vars = 'Species')
u dcast()函数:参数设置:(文件名,公式=标识变量~操作变量,汇总函数=mean,value.var = 需要进行汇总的变量)#dcast()非常强大的函数
dcast(new_iris,formula = Species-variable,fun.aggregate = mean,value.var = 'value')
u tips数据集示例
dcast(tips,formula = sex~.,fun.aggregate = mean,value.var = 'tip') #给小费与性别的关系 (.点表示占位符,因为只有一个待比较的变量)
sex .
1 Female 2.833448
2 Male 3.089618
dcast(data = tips,formula = sex~smoker,fun.aggregate = mean,value.var = 'tip') #给小费与性别和抽烟与否的关系
sex No Yes
1 Female 2.773519 2.931515
2 Male 3.113402 3.051167
变量的因子化 (即把连续的变量转换为分类变量)
n 公式法
u 示例1:
> age <- sample(20:80,20)
> age
[1] 49 64 63 75 74 79 45 66 28 76 60 33 39 77 35 44 31 38 24 53
> age1 <- 1+ (age >30) +(age >40) +(age > 50)
> age1
[1] 3 4 4 4 4 4 3 4 1 4 4 2 2 4 2 3 2 2 1 4
> age_fac <- factor(age1,labels = c('young','middle','m-old','old'))
> age_fac
[1] m-old old old old old old m-old old young old old middle middle
[14] old middle m-old middle middle young old
Levels: young middle m-old old
u 示例2:与示例1达到相同的结果
> age2 <- 1*(age < 30) + 2*(age >=30 & age < 40) + 3*(age >=40 & age <50)+4*(age>=50)
> age2
[1] 3 4 4 4 4 4 3 4 1 4 4 2 2 4 2 3 2 2 1 4
n cut()法:很常用
u 示例1:
> age3 <- cut(age,breaks = 4,labels = c('young','middle','m-old','old'),include.lowest = TRUE,
+ right = TRUE)
> age3
[1] middle m-old m-old old old old middle old young old m-old young middle old
[15] young middle young middle young m-old
Levels: young middle m-old old
u 示例2:
> age4 <- cut(age,breaks = seq(20,80,length.out = 4),labels = c('young',
+ 'middle','old'))
> age4
[1] middle old old old old old middle old young old middle young young old
[15] young middle young young young middle
Levels: young middle old
n ifelse()函数:参数设置test是指待用于检验的元素,第二个参数代表检验值为真(yes),第三个参数代表检验值为假(false)。很好用,很常用
u 示例1:
> ifelse(age > 50,'old','young')
[1] "young" "old" "old" "old" "old" "old" "young"
[8] "old" "young" "old" "old" "young" "young" "old"
[15] "young" "young" "young" "young" "young" "old"
u 示例2:
> ifelse(age >60,'old',ifelse(age <30,'young',ifelse ((age >= 30 & age < 45),'m-young','m-old')))
[1] "m-old" "old" "old" "old" "old"
[6] "old" "m-old" "old" "young" "old"
[11] "m-old" "m-young" "m-young" "old" "m-young"
[16] "m-young" "m-young" "m-young" "young" "m-old"
n car扩展包中的recode()函数:参数设置,待变量,recode为重新编码规则
u 示例
> recode(var = age, recode ='20:29 = 1;30:39 = 2;40:49 = 3;50:hi = 4')
[1] 3 4 4 4 4 4 3 4 1 4 4 2 2 4 2 3 2 2 1 4