如何在不增删列的情况下合并两个表

How to merge two tables without adding and deleting columns

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


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)

也许你可以向 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

我们可以在 data.table

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