简体   繁体   English

结合多个条件进行变异 - R dyplr

[英]mutate combining multiple conditions - R dyplr

I have the following first rows of a dataframe:我有 dataframe 的以下第一行:

response_accuracy = data.frame (trial_number  = c("108", "108", "108", "108","108",
                                                  "108", "111", "111", "111", "111",
                                                  "111", "111","112", "112","112",
                                                  "112","112", "112"),
                                response_time_to_origine = c("19516.990", "19516.990",
                                                             "19516.990", "19516.990",
                                                             "19516.990", "19516.990",
                                                             "21364.9990", "21364.990",
                                                             "21364.9990", "21364.990",
                                                             "21364.9990", "21364.990",
                                                             "22068.990", "22068.990",
                                                             "22068.990", "22068.990",
                                                             "22068.990", "22068.990"),
                                block = c("1","1","2","2","3","3", "1","1","2","2",
                                          "3","3","1","1","2","2","3","3"),
                                subblock = c("A","B","A","B","A","B","A","B",
                                             "A","B","A","B","A","B","A","B",
                                             "A","B"),
                                target_onset = c("18893.7100", "1299.7971", 
                                                 "4802.5891", "16355.1118",
                                                 "18766.6272", "24800.6479",
                                                 "20717.6990", "7688.3330",
                                                 "15868.4771", "16355.1118",
                                                 "23899.7302", "16067.6699",
                                                 "20717.6990", "27016.7981",
                                                 "23333.1860", "23083.0769",
                                                 "18766.6272", "16067.6699"),
                                target_onset_plus200 = c("19093.7100","1499.7971",
                                                         "5002.5891", "16555.1118", 
                                                         "18966.6272","25000.6479", 
                                                         "20917.6990","7888.3330", 
                                                         "16068.4771","16555.1118",
                                                         "24099.7302","16267.6699",
                                                         "20917.6990","27216.7981",
                                                         "23533.1860","23283.0769",
                                                         "18966.6272","16267.6699"), 
                                target_onset_plus1000 = c("19893.710","2299.797",
                                                          "5802.589","17355.112",
                                                          "19766.627","25800.648",
                                                          "21717.699","8688.333",
                                                          "16868.477","17355.112", 
                                                          "24899.730","17067.670",
                                                          "21717.699","28016.798",
                                                          "24333.186","24083.077",
                                                          "19766.627","17067.670"))

Now, I would like to know whether, for each trial_number, the value in response_time_to_origin is contained between target_onset_plus200 and target_onset_plus1000 but according to multiple conditions:现在,我想知道对于每个 trial_number,response_time_to_origin 中的值是否包含在 target_onset_plus200 和 target_onset_plus1000 之间,但根据多个条件:

if 76 <= trial_number <= 151, filter block == 1 and subblock == "A" and then check whether response_time_to_origin is between target_onset_plus200 and target_onset_plus1000. if 76 <= trial_number <= 151, filter block == 1 and subblock == "A" 然后检查 response_time_to_origin 是否在 target_onset_plus200 和 target_onset_plus1000 之间。 If so answer TRUE, if this happens for other blocks and subblock, answer FALSE.如果是,则回答 TRUE,如果其他块和子块发生这种情况,则回答 FALSE。

else if 152 <= trial_number <= 227, filter block == 1 and subblock == "B" and then check whether response_time_to_origin is between target_onset_plus200 and target_onset_plus1000. else if 152 <= trial_number <= 227, filter block == 1 and subblock == "B" 然后检查 response_time_to_origin 是否在 target_onset_plus200 和 target_onset_plus1000 之间。 If so answer TRUE, if this happens for other blocks and subblock, answer FALSE.如果是,则回答 TRUE,如果其他块和子块发生这种情况,则回答 FALSE。

ETC.等等。

I have tried to insert the following bit of code, which obviously does not work:我试图插入以下代码,这显然不起作用:

response_accuracy=data_out%>%     
   select(trial_number, response_time_to_origin)%>%     
   filter(!is.na(response_time_to_origin))%>%     
   mutate(list_windows_terminals = list(list_hit_windows))%>%      
   unnest(list_windows_terminals)%>%     
   group_by(block, subblock, trial_number)%>%     
   mutate(response_in_window = if(76 <= trial_number <= 151){     
      filter(block == 1 & subblock == "A") & response_time_to_origin >      
      target_onset_plus200 & response_time_to_origin < 
      target_onset_plus1000)})

And neither does this one:这个也没有:

response_accuracy=data_out%>%
    select(trial_number, response_time_to_origin)%>%
    filter(!is.na(response_time_to_origin))%>%
    mutate(list_windows_terminals = list(list_hit_windows))%>% 
    unnest(list_windows_terminals)%>%
    group_by(block, subblock, trial_number)%>%
    mutate(response_in_window = ifelse(between(trial_number, 76, 151) && block == 1 && subblock == "A", "TRUE", "FALSE")) %>%
    mutate(response_in_window = ifelse(between(trial_number, 152, 227) && block == 1 && subblock == "B", "TRUE", "FALSE"))%>%
    mutate(response_in_window = ifelse(between(trial_number, 228, 303) && block == 2 && subblock == "A", "TRUE", "FALSE")) %>%
    mutate(response_in_window = ifelse(between(trial_number, 304, 379) && block == 2 && subblock == "B", "TRUE", "FALSE"))%>%
    mutate(response_in_window = ifelse(between(trial_number, 380, 455) && block == 3 && subblock == "A", "TRUE", "FALSE")) %>%
    mutate(response_in_window = ifelse(between(trial_number, 456, 531) && block == 3 && subblock == "B", "TRUE", response_in_window))

Could you help me?你可以帮帮我吗? Thanks a lot in advance:非常感谢:

You can use a case_when to build up the logic for each of your conditions.您可以使用 case_when 为每个条件构建逻辑。 See example below with your first condition included.请参阅下面的示例,其中包含您的第一个条件。 You can add the other conditions as new lines in your case_when statement.您可以在 case_when 语句中将其他条件添加为新行。

output <- response_accuracy %>%
  mutate(response_time_to_origine = as.numeric(response_time_to_origine),
         target_onset_plus200 = as.numeric(target_onset_plus200),
         target_onset_plus1000 = as.numeric(target_onset_plus1000),
         trial_number = as.numeric(trial_number),
         block = as.numeric(block),
         target_onset = as.numeric(target_onset)) %>%
  mutate(Flag1 = ifelse(response_time_to_origine > target_onset_plus200 & 
                          response_time_to_origine < target_onset_plus1000, 
                        T, F),
         answer = 
           case_when(
             Flag1 & trial_number >76 & trial_number < 151 & block == 1 & subblock == "A" ~ "Meets Conditions",
             T ~ "Does Not Meet Conditions"
           ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM