重复data.frame,添加主键

标签 r dplyr

我有一个数据框。说,

data.frame(x = c(1, 3), y = c(5, 0), id = c("A", "B"))

现在我想复制它,这样我在同一个 data.frame 中有一个副本。我最终会得到这样的结果,

 data.frame(x = c(1, 3, 1, 3), y = c(5, 0, 5, 0), id = c("A", "B", "A", "B"))

现在,这非常接近我想要的,但我还想附加 id 列,根据我想要的重复项数量使它们对每一行都是唯一的(在本例中只有一个,但我想要 n 个) 。

data.frame(x = c(1, 3, 1, 3), y = c(5, 0, 5, 0), id = c("A-1", "B-1", "A-2", "B-2"))

因此,正如您所看到的,我可以全神贯注于制作对象,但我想从使用基础 R 编写“hacky”代码转向使用 dplyr 复制此功能。

最佳答案

所以我注意到您想使用 dplyr 包来执行此操作。我认为使用 dplyr 中的 group_by()mutate()row_number() 函数的组合,你可以很好地完成这项工作。

library(dplyr)

# so you start with this data.frame:
df <- data.frame(x = c(1, 3), y = c(5, 0), id = c("A", "B"))

# to attach an exact duplication of this df to itself:
df <- rbind(df, df)


# group by id, add a second id to increment within each id group ("A", "B", etc.)
df2 <- group_by(df, id) %>%
    mutate(id2 = row_number())


# paste the id and id2 together for the desired result
df2$id_combined <- paste0(df2$id, '-', df2$id2)

# inspect results
df2
    # x     y     id   id2 id_combined
    # <dbl> <dbl> <fctr> <int>       <chr>
    # 1     1     5      A     1         A-1
    # 2     3     0      B     1         B-1
    # 3     1     5      A     2         A-2
    # 4     3     0      B     2         B-2

请记住,您现在拥有“tibble”/“分组 data.frame”,而不是基本 data.frame。

如果您愿意,可以很简单地将其恢复为原始 data.frame。

df2 <- data.frame(df2, stringsAsFactors = F)

# now to remove the additional columns that were added in this process:
df2$id2 <- NULL

编辑 - 探索将同一数据帧的 n 个副本附加到自身的其他选项:

# Not dplyr, but this is how I would normally handle this type of task:
df <- data.frame(x = c(1, 3), y = c(5, 0), id = c("A", "B"))

# set n equal to the number of times you want to replicate the data.frame
n <- 13

# initialize space to hold the data frames
list_dfs <- list()

# loop through, adding individual data frames to the list
for(i in 1:n) {
    list_dfs[[i]] <- df
}

# combine them all with do.call
my_big_df <- do.call(rbind, list_dfs)

从那里,您可以使用 group_by()mutate()row_number() 函数,如我上面所示为 data.frame 创建了新的唯一键。

关于重复data.frame,添加主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42894086/

相关文章:

R gsub 从 x 列中的单词中删除 y 列中的单词

r - tidyR嵌套并完成: avoid group_by (or data table equivalent)

重复将条件摘要应用于数据框中的组

r - 使用 dplyr 在数据库中写入表

R 错误 "Can' t join on ... 因为类型不兼容”

R,使用 load() 从 .rda 对象分配内容

R mutate 不会添加列

r - 将指定的列转置为具有分组数据的行

r - 如何在for循环中引用变量?

python - 如何使用 Python 根据数据集的列查找动态函数的根