如何在不添加和删除列的情况下合并两个表

时间:2021-03-05 10:23:40

标签: r dataframe merge rename

我有两个表,其中包含我想将测试用例作为关键字加入的信息。我可以先加入它们,然后重命名列,然后重新排序数据框,但有没有更优雅的方法?


df1 <- data.frame(
  testcase = c('testcase1', 'testcase2', 'testcase3', 'testcase4', 'testcase5'), 
  passed = c('2', '0', '2', '0', '0'), 
  failed = c('0', '2', '2', '0', '2'))

df2 <- data.frame(
  id = c(1:10), testid = c('testcase3', 'testcase1', 'testcase3',   'testcase2', 'testcase5', 'testcase1', 
  'testcase3', 'testcase5', 'testcase2', 'testcase3'), total_passed = rep("", 10), total_failed= rep("", 10), testid = c(510:519), total_items = rep("", 10))

我的解决方案如下,但可以用更少的步骤完成吗?

df3 <- merge(df2, df1, by.x='testid', by.y='testcase')
df3$total_passed <- df3$total_failed <- NULL
df3$total_items <- 10
df3 <- select(df3, id, testid, total_passed = passed,  total_failed= failed, testid, total_items)

2 个答案:

答案 0 :(得分:1)

也许您可以借助 dplyr 库:

library(dplyr)

df2 %>%
  inner_join(df1, by = c('testid' = 'testcase')) %>%
  transmute(id, testid, total_passed = passed, total_failed = failed, 
            total_items = 10)

#   id    testid total_passed total_failed total_items
#1   1 testcase3            2            2          10
#2   2 testcase1            2            0          10
#3   3 testcase3            2            2          10
#4   4 testcase2            0            2          10
#5   5 testcase5            0            2          10
#6   6 testcase1            2            0          10
#7   7 testcase3            2            2          10
#8   8 testcase5            0            2          10
#9   9 testcase2            0            2          10
#10 10 testcase3            2            2          10

答案 1 :(得分:1)

我们可以在 data.table

中使用连接
library(data.table)
setDT(df2)[df1, c('total_passed', 'total_failed', 'total_items')
         := .(passed, failed, 10), on = .(testid = testcase)]
相关问题