I have a dataframe where I would like to fill in values from column "reference column" into the column "trial_number" based on the following two conditions:
- condition 1: value column "start_time" >= value column "a" and
- condition 2: value column "end_time" <= value column "b"
The values should only be filled into column "trial_number" if both conditions are met at the same time.
One complication is that the columns "start_time" and "end_time" contain more values than columns "a", "b" and "reference_column". The idea is that each of the values in "reference_column" is filled in multiple rows of column "trial_number".
I as well have the columns "a", "b" and "reference_column" in a separate dataframe.
A simplified version of the dataframe looks like this:
reference_column a b trial_number start_time end_time
1 1 10 20 NA 12 18
2 2 22 24 NA 22 23
3 3 30 40 NA 23 24
4 4 45 55 NA 31 32
5 NA NA NA NA 35 36
6 NA NA NA NA 36 37
7 NA NA NA NA 37 39
8 NA NA NA NA 46 51
9 NA NA NA NA 47 53
10 NA NA NA NA 50 54
In the end, the desired output (filled values in "trial_number" should look like this:
reference_column a b trial_number start_time end_time
1 1 10 20 01 12 18
2 2 22 24 02 22 23
3 3 30 40 02 23 24
4 4 45 55 03 31 32
5 NA NA NA 03 35 36
6 NA NA NA 03 36 37
7 NA NA NA 03 37 39
8 NA NA NA 04 46 51
9 NA NA NA 04 47 53
10 NA NA NA 04 50 54
Since I am quite new to R, I would be very happy about any recommendations. I already tried to use a for loop in combination with an if-statement, but wasn't successful because of the unbalanced number of data entries and various conditions. Thanks a lot in advance!