将数据帧合并/堆叠在一起,并将相应的表名合并到第一列的变量中

时间:2014-05-25 05:02:37

标签: r

我有多个遵循这种格式的数据框:

customers_outdoor
id rating
12 1
21 2 
45 3

customers_sports
id rating
23  1
34  2

我想将它们全部合并到rbind方式的一个数据框中,并且要求id变量成为id列与表名的串联。理想情况下应如下所示:

customers_all
id  rating
customers_outdoor.12 1
customers_outdoor.21 2
customers_outdoor.45 3
customers_sports.23 1
customers_sports.34 2

在没有太多循环的情况下寻找一种简单的方法。谢谢!

2 个答案:

答案 0 :(得分:2)

这实际上不称为 合并 (即组合列)。您将行绑定在一起,有时称为堆叠

首先在每个标识源的数据框中添加一列:

customers_outdoor$df_name <- "customers_outdoor"
customers_sports$df_name <- "customers_sports"

堆叠起来

customers_all <- rbind(customers_outdoor, customers_sports)

创建新的ID列:

customers_all$new_id <- with(customers_all, paste(df_name, id, sep = ".")

您可以删除和重命名列,以获得customers_all所需的格式。

答案 1 :(得分:2)

这是另一种方法,但我更喜欢user3114046:

## customers_outdoor <- read.table(text="
## id rating
## 12 1
## 21 2 
## 45 3", header=TRUE)
## 
## customers_sports <- read.table(text="
## id rating
## 23  1
## 34  2", header=TRUE)


L1 <- setNames(list(customers_outdoor, customers_sports),
    c("customers_outdoor", "customers_sports"))

dat <- data.frame(x=rep(names(L1), sapply(L1, nrow)),
    do.call(rbind.data.frame, L1), row.names=NULL)

data.frame(id=paste(dat[, 1], dat[, 2], sep="."), dat[, 3, drop=FALSE])

##                     id rating
## 1 customers_outdoor.12      1
## 2 customers_outdoor.21      2
## 3 customers_outdoor.45      3
## 4  customers_sports.23      1
## 5  customers_sports.34      2