0

我正在尝试组合两个数据框,但创建一个包含每个数据框名称的一部分的列,以便我可以识别每个数据来自哪个数据框。数据框是

Acme_types_number <- structure(list(type = structure(c(3L, 1L, 2L, 4L, 3L), .Label = c("blue", "green", "red", "yellow"), class = "factor"), number = c(4L, 6L, 54L, 5L, 5L)), .Names = c("type", "number"), class = "data.frame", row.names = c(NA, -5L))

Acme2_types_number <- structure(list(type = structure(c(2L, 3L, 1L, 4L, 3L), .Label = c("blue", "green", "red", "yellow"), class = "factor"), number = c(4L, 5L, 6L, 5L, 5L)), .Names = c("type", "number"), class = "data.frame", row.names = c(NA, -5L))

我可以使用将这些组合成一个数据框

df <- rbind(Acme_types_number, Acme2_types_number)

它给出了以下数据框df:

type    number
red         4
blue        6
green       54
yellow      5
red         5
green       4
red         5
blue        6
yellow      5
red         5

但是,我还想添加一列,其中包含每个数据框名称的第一部分(例如,下划线之前的部分),以给出:

factory   type number
   Acme    red      4
   Acme   blue      6
   Acme  green     54
   Acme yellow      5
   Acme    red      5
  Acme2  green      4
  Acme2    red      5
  Acme2   blue      6
  Acme2 yellow      5
  Acme2    red      5

有谁知道如何在 R 中做到这一点?

4

1 回答 1

2

像那样:

rbind(data.frame(factory = "Acme", Acme_types_number),
      data.frame(factory = "Acme2", Acme2_types_number))
于 2013-06-25T02:17:28.627 回答