我正在尝试组合两个数据框,但是创建一个包含每个数据框名称的一部分的列,以便我可以识别每个数据来自哪个数据框。数据框是
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中执行此操作吗?
答案 0 :(得分:2)
就像那样:
rbind(data.frame(factory = "Acme", Acme_types_number),
data.frame(factory = "Acme2", Acme2_types_number))