【发布时间】:2021-06-04 00:43:42
【问题描述】:
我有两个表,其中包含我想以测试用例作为键加入的信息。我可以先加入它们,然后重命名列,然后重新排序数据框,但是有没有更优雅的方法?
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)
【问题讨论】: