I have a question about manipulating data frames taking into account row indexes. I have asked this in Stack Exchange, but I haven't had any luck finding a
tidyverse solution yet. So I'd very much appreciate your suggestions!
I am trying to summarize my dataframe in a way that makes reference to row numbers, but I can't find a way to do it in a tidy way. My dataframe looks like this.
# Sample data frame. df <- data.frame(value = c(1,2,1,1,2,4,5,3,2)) value 1 1 2 2 3 1 4 1 5 2 6 4 7 5 8 3 9 2
I need to create a column, which says
TRUE if the corresponding number in
value as well as the numbers in next 4 consecutive rows are all larger than or equal to 2. The resulting dataframe should look like this:
value largerThan 1 1 FALSE 2 2 FALSE 3 1 FALSE 4 1 FALSE 5 2 TRUE 6 4 NA 7 5 NA 8 3 NA 9 2 NA
Note the four
NA in the last four rows of
largerThan. This is because these these rows don't have 4 consecutive rows after them, so they can't be evaluated. This is what is
tripping me up, together with the fact that I don't know how to make reference to row numbers when using
tidyverse syntax. This was more straightforward with
for loops, but I can't think of equivalents with the