绑定许多数据框,并为其ID添加一列 [英] bind many data frames adding a column with their id
问题描述
我有很多重复命名的数据帧:
df.1 <-data.frame( x = c (1,2), y = 2)
df.2<-data.frame( x = c(2,4), y = 4)
df.3 <-data.frame( x = 2, y = c(4,5))
所有数据帧具有相同数量的行和列。
我想绑定它们,并添加带有数据框ID的列。该ID将是源数据帧的名称。
我知道我可以手动执行此操作:
rbind(data.frame(id = df.1,df.1),
data.frame(id = df.2,df.2),
data.frame(id = df.3,df.3))
但是
我尝试编写循环,但是它们没有用。
我想那是因为我将它们基于包含数据帧名称的字符串列表而不是数据帧本身的列表。
df_names<-ls(pattern = df.\\d +)
for(i in df_names){
i $ id<--i
i
}
...但是我也没有找到创建列表的任何自动方法具有可重复名称的数据帧。即使我这样做,也不确定上面的for循环是否可以工作:)
您可以使用 parse
和 eval
从 df_names
获取数据帧:
do.call(rbind,lapply(df_names,function(x){data.frame(id = x,eval(parse( text = x))}}))
id xy
1 df.1 1 2
2 df.1 2 2
3 df。 2 2 4
4 df.2 4 4
5 df.3 2 4
6 df.3 2 5
I have many data frames named repeatably:
df.1 <- data.frame("x"=c(1,2), "y"=2)
df.2 <- data.frame("x"=c(2,4), "y"=4)
df.3 <- data.frame("x"=2, "y"=c(4,5))
All data frames have the same number of rows and columns. I want to bind them, adding a column with the id of the data frame. The id would be the name of the source data frame.
I know I could do this manually:
rbind(data.frame(id = "df.1", df.1),
data.frame(id = "df.2", df.2),
data.frame(id = "df.3", df.3))
But there's a lot of them and their number will change in the future.
I tried writing for loops but they didn't work. I suppose that's because I'm basing them on a list of strings containing data frames' names rather than a list of data frames themselves.
df_names <- ls(pattern = "df.\\d+")
for (i in df_names) {
i$id <- i
i
}
...but I also haven't found any automated way of creating a list of data frames with repeatable names. And even if I do, I'm not that sure the for-loop above would work :)
You could use parse
and eval
to get the data frames from df_names
:
do.call(rbind, lapply(df_names, function(x){data.frame(id=x, eval(parse(text=x)))}))
id x y
1 df.1 1 2
2 df.1 2 2
3 df.2 2 4
4 df.2 4 4
5 df.3 2 4
6 df.3 2 5
这篇关于绑定许多数据框,并为其ID添加一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!