标签:
工作笔记记录,会持续更新....
目录:
apply函数:
> ma <- matrix(c(1:4, 1, 6:8), nrow = 2);ma [,1] [,2] [,3] [,4] [1,] 1 3 1 7 [2,] 2 4 6 8 > apply(ma, 1, sum) [1] 12 20 > apply(ma, 2, sum) [1] 3 7 7 15
tapply:
> da year province sale 1 2007 A 1 2 2007 B 2 3 2007 C 3 4 2007 D 4 5 2008 A 5 6 2008 C 6 7 2008 D 7 8 2009 B 8 9 2009 C 9 10 2009 D 10 > attach(da) > tapply(sale,list(year,province),mean) A B C D 2007 1 2 3 4 2008 5 NA 6 7 2009 NA 8 9 10
lapply:
#外卖示例数据 s<-data.frame(shop_id=c(‘001‘,‘002‘),saled=c(35,78), takeout_cost=c(30,60),delivery_time=c(10,20)) lapply(s,function(x){ if(is.numeric(x)){ mean(x) } else{ length(x) } })
sapply:
shop_id saled takeout_cost delivery_time 2.0 56.5 45.0 15.0
merge:
#数据: w1: NAME SCHOOL CLASS ENGLISH A S1 10 85 B S2 5 50 A S1 4 90 A S1 11 90 C S1 1 12 w2: NAME SCHOOL CLASS MATHS ENGLISH A S3 5 80 88 B S2 5 89 81 C S1 1 55 32 merge(w1, w2, all = T) NAME SCHOOL CLASS ENGLISH MATHS 1 A S1 4 90 NA 2 A S1 10 85 NA 3 A S1 11 90 NA 4 A S3 5 88 80 5 B S2 5 50 NA 6 B S2 5 81 89 7 C S1 1 12 NA 8 C S1 1 32 55 merge(w1, w2, by = c("NAME", "SCHOOL", "CLASS"), all = T) NAME SCHOOL CLASS ENGLISH.x MATHS ENGLISH.y 1 A S1 4 90 NA NA 2 A S1 10 85 NA NA 3 A S1 11 90 NA NA 4 A S3 5 NA 80 88 5 B S2 5 50 89 81 6 C S1 1 12 55 32
标签:
原文地址:http://www.cnblogs.com/tgzhu/p/5776389.html