【问题标题】:How to make if-else-sentences by combining info from two different tables如何通过组合来自两个不同表的信息来制作 if-else-sentences
【发布时间】:2021-03-04 20:20:48
【问题描述】:

我有一个测试用例表

test_table <- data.frame(
  id = c(1:10), 
  result = c('passed:testcase3', 'passed:testcase1', 'failed:testcase3', 
  'failed:testcase2', 'failed:testcase5', 'passed:testcase1', 
  'failed:testcase3', 'failed:testcase5', 'failed:testcase2', 'passed:testcase3')
)

还有一份我想通过匹配 test_table 中的信息来填写的报告

report <- data.frame(
  testcase = c('testcase1', 'testcase2', 'testcase3', 'testcase4', 'testcase5'), 
  passed = rep("", 5), failed= rep("", 5)
)

我正在尝试通过 ifelse-sentences 填写通过和失败的测试用例的数量,但我找不到正确的设置。

#I create a testcase variable
test_table$testcase <- sub('.+:(.+)', '\\1', test_table$result)

#I try to fill out the missing fields in the report by:

report$passed <- ifelse(test_table$testcase==report$testcase, 
  count(test_table$result '%like%' 'passed')))
report$failed <- ifelse(test_table$testcase==report$testcase,
  count(test_table$result '%like%' 'failed')))

预期结果是:

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

【问题讨论】:

    标签: r dataframe if-statement


    【解决方案1】:

    你可以用一点dplyrtidyr来做到这一点

    library(dplyr)
    library(tidyr)
    test_table %>% 
      separate(result, sep=":", into=c("result", "case")) %>% 
      group_by(case) %>% 
      count(result) %>% 
      pivot_wider(names_from=result, values_from=n, values_fill=0)
    

    或者

    test_table %>% 
      tidyr::separate(result, sep=":", into=c("result", "case")) %>% 
      group_by(case) %>% 
      summarize(passsed=sum(result=="passed"), failed=sum(result=="failed"))
    

    或者使用基础R

    test_table$testcase <- sub('.+:(.+)', '\\1', test_table$result)
    test_table$outcome <- sub('(.+):(.+)', '\\1', test_table$result)
    
    with(test_table, table(testcase, outcome))
    

    (注意最后一个选项实际上返回的是一个表而不是一个data.frame)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多