R过滤逻辑

时间:2015-03-09 20:19:38

标签: r filter logic conditional-statements

需要了解如何修复此过滤逻辑。

test.session <- c("A","A","A","B","B","B")
test.start_flag <- c("1","0","0","1","0","0")
test.url <- c("in_list","in_field","out","not_in_list","in_field","out")
test.code_map <- 'tbd'
test.manual_map <- c("1","1","0","0","0","0")
test.df <- data.frame(test.session, test.start_flag, test.url, test.code_map, test.manual_map)

我想生成test.code_map以匹配test.manual_map向量。我编写了前四行,但逻辑需要针对第5行和第6行进行调整。逻辑:

  • 如果开始标志为1且网址为&#39; in_list&#39;,则code_map等于1(已完成)
  • 如果开始标志为0且网址为&#39; in_field&#39;,则code_map等于1(已完成)
  • 如果开始标志为0且网址为“退出”字样。然后code_map等于0(完成)
  • 如果开始标志为1且网址为&#39; not_in_list&#39;然后代码映射等于0(完成)
  • 如果开始标志为0且网址为“退出”字样。然后代码映射等于0(完成 - 与上面相同)
  • 如果开始标志为0且网址为&#39; in_field&#39;,则代码映射等于0 ***需要逻辑调整

***此字段需要等于0,因为在此特定会话(会话B)中,起始URL不等于我定义的字段,因此code_map的所有值都必须等于0只有在满足这一特定条件时才会这样做。

1 个答案:

答案 0 :(得分:0)

理解你想要的东西有点令人困惑,但似乎测试会话有一个test.url&#34; not_in_list&#34;,那么该测试会话的所有test.code_maps应该是0.如果是这样,这应该有效

test.df$test.code_map<-ifelse(
  ((test.df$test.start_flag == 1 & test.df$test.url=="in_list")|
  (test.df$test.start_flag == 0 & test.df$test.url=="in_field"& test.df$test.session!="B") &
    !test.session %in% test.df$test.session[test.df$test.url=="not_in_list"]),
  1, 0)