将函数映射到数据帧列表

时间:2017-01-16 15:09:26

标签: r

在R中,我可以使用lapplymap$year列添加到列表中的多个数据框吗?我希望在融化它们之前在数据框中有这一年。

years<-2004:2006
names04<-c("green","q22","yellow")
names05<-c("green","q27","yellow")
names06<-c("green","q29","yellow")
yr2004<-1:3
yr2005<-6:8
yr2006<-8:10

year2004<-rbind(names04,yr2004)
colnames(year2004)<-names04
year2004
year2005<-rbind(names05,yr2005)
colnames(year2005)<-names05
year2006<-rbind(names06,yr2006)
colnames(year2006)<-names06
year2004<-as.data.frame(year2004)
year2005<-as.data.frame(year2005)
year2006<-as.data.frame(year2006)

## rewrite this section with lapply ##
dfs<-list(year2004,year2005,year2006)
year2004$year=2004
year2005$year=2005
year2006$year=2006

library(purrr)
map(cbind,dfs,years)

#where
years <- c(2004, 2005, 2006)
  

map给出错误:.x不是向量(闭包)

1 个答案:

答案 0 :(得分:2)

将数据框放入列表后,您可以使用年份创建一个向量并使用$('.imageholder').not(this) ,即

Map

请注意,您在问题中使用的dfs <- list(year2004, year2005, year2006) years <- c(2004, 2005, 2006) Map(cbind, dfs, year = years) #[[1]] # green q22 yellow year #names04 green q22 yellow 2004 #yr2004 1 2 3 2004 #[[2]] # green q27 yellow year #names05 green q27 yellow 2005 #yr2005 6 7 8 2005 #[[3]] # green q29 yellow year #names06 green q29 yellow 2006 #yr2006 8 9 10 2006 来自map,因此错误