R-在循环中引用不同的数据帧 [英] R- Referencing different dataframes in a loop
问题描述
我在想:
for(y in 1987:2008){
paste(Plant,y,sep =)$ year < - y
}
这不起作用,因为粘贴显然不是正确的功能。有没有一个聪明,快捷的方法来做到这一点?谢谢
试试这个..
year = seq(1987,2008,by = 1)
list_object_names = sprintf(Plant%s,1987:2008)
list_DataFrame = lapply(list_object_names,get)
for(in 1:length(list_DataFrame)){
list_DataFrame [[i]] [,'Year'] = year [i]
}
I am brand new to R so if I'm thinking about this completely wrong feel free to tell me. I have a series of imported dataframes on power plants, one of each year (Plant1987, Plant1988 etc...) that I am trying to combine ultimately into one data frame. Prior to doing so, I'd like to add a "year" variable to each dataframe. I could do this for each individual dataframe, but would like to formalize it and do it in one step. I know how to do it in stata, but I'm struggling here.
I was thinking something along the lines of:
for (y in 1987:2008) {
paste("Plant",y,sep="")$year <- y
}
which doesn't work because paste is obviously not the right function. Is there a smart, quick way to do this? Thanks
Try this ..
year=seq(1987,2008,by=1)
list_object_names = sprintf("Plant%s", 1987:2008)
list_DataFrame = lapply(list_object_names, get)
for (i in 1:length(list_DataFrame ) ){
list_DataFrame[[i]][,'Year']=year[i]
}
这篇关于R-在循环中引用不同的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!