2017-01-16 186 views
-1

在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 gives an error: .x is not a vector (closure)

+2

把它们放在一个列表和地图? 'Map(cbind,l1,year = years)'year where c(2004,5,6)and l1 the list with your df – Sotos

回答

2

一旦你把数据帧中的列表,你可以创建一个多年的载体和使用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您使用的是你的问题是从purrr(package),因此错误