假设我在当前R环境中有3个数据帧,命名为d1f,df2,df_3。他们的名字没有模式。如何按名称访问一个数据框?
例如,我有一个for循环来处理三个数据帧。我怎么能这样做?
df_names<-c("d1f", "df2", "df_3")
for(name in df_names)
{
df<-some_function(name)
....some action on df....
}
答案 0 :(得分:0)
最好将数据帧存储在如下列表中:
set.seed(1)
d1f = rnorm(10)
df2 = rnorm(10)
df_3 = rnorm(10)
dfs = list(d1f, df2, df_3)
for (i in 1:length(dfs)){
dfs[[i]] = dfs[[i]] +1 # eg. add 1 to each element of the three data frames
}