I have a list of 106 tibbles, each one contains two columns (date, temperature) with thousands of values.
I tried to create a function that allows me to get the index of the row, in which the temperature is lower than 8.0 four times by tibble.
The problem I am having is that my code, is performing only the first row of every single tibble.
Here you can see the code:
pos_r = 0;
temp =0;
posx = vector();
for (i in seq_along(data_sensor)){
if (temp < 4){
pos_r = pos_r + 1;
if (data_sensor[[i]]$Temperature < 8.0){
temp=temp+1;
} else if (temp == 4){
posx[i] = pos_r;
i = i+1;
}
}
}
> [1] NA NA NA NA NA NA 5 6 NA 7 8 NA NA 9 NA NA NA 10 11 NA 12 13 14 NA 15 16 17 18 19 NA
[31] 20 21 22 NA 23 24 25 26 27 NA 28 NA 29 30 NA 31 32 33 34 NA 35 36 37 38 NA 39 40 41 42 43
[61] 44 NA 45 NA 46 47 48 49 50 51 52 53 54 55 56 57 58 NA NA NA 59 60 61 NA 62 63 NA 64 65 66
[91] NA 67 NA NA 68 69 70 71 72 73 74 75 76 77 78 79
How can I treat all the rows of every single tibble of the list?
Here's one option: In the code below we use logical tests to find the index of the row for which temperature has been below 8 on four days. Then we use
mapto implement this method on each data frame in the list.Here are the individual steps illustrated on the first data frame in the list:
Get the indicated row from each data frame, or missing values if there's no row that satisfied the condition. Return the result as a data frame: