R过滤逻辑

R filtering logic

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

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 行调整逻辑。逻辑:

*** 该字段需要等于 0,因为在此特定会话(会话 B)中,起始 URL 不等于我定义的字段,因此 code_map只有满足这个特定条件才需要等于0。

理解你想要什么有点混乱,但似乎如果测试会话有 test.url "not_in_list",那么该测试会话的所有 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)