R - multiple conditions within mutate function

Viewed 69

I have the following dataframe (see the link : response_accuracy). 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 :

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 so answer TRUE, if this happens for other blocks and subblock, answer 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. If so answer TRUE, if this happens for other blocks and subblock, answer 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)})

Could you help me ? Thanks in advance !

1 Answers

It makes it a bit difficult to help you without the minimal example of data.

Try this code:

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", response_in_window))

If it doesn't work, you should provide a code to generate your dataframe for me to test.

Related