Home > OS >  Nested for loop between 2 data frames
Nested for loop between 2 data frames

Time:03-30

I would like to do the following process : I have a vector called response_time_to_origin on a side (see the link : response_time_to_origin) and on another side a data frame called list_hit_windows (see the link : list_hit_windows) containing 3 columns, ie. target_onset, target_onset_plus200 and target_onset_plus1000. At each row of this data frame, target_onset_plus200 and target_onset_plus1000 should be considered as interval terminals.

Through a bit of code, I would like to know whether, for each row of response_time_to_origin, the value is contained between target_onset_plus200 and target_onset_plus1000 at one or more rows of the list_hit_window data frame (if yes, return TRUE, if no return FALSE).

How should I do that ?

CodePudding user response:

In general terms I would add list_hit_windows as a new list column in response_time_to_origin. I would then unnest the list column to give you a repeat of list_hit_windows for each value of response_time_to_origin. You can then create a dummy variable as to whether each row meets your criteria

df %>% mutate(meets_criteria = response_time_to_origin > target_onset_plus200 & response_time_to_origin < target_onset_plus1000)

That will give you your TRUE and FALSE column as requested.

You could have a look at the last bit of this answer R - calculate daily/weekly rate with changing denominator which is similar and reproducible for you to learn from.

  • Related