Here is a df for example:
test_df <- structure(list(plant_sp = c("plant_1", "plant_1", "plant_2", "plant_2", "plant_3",
"plant_3", "plant_3", "plant_3", "plant_3", "plant_4",
"plant_4", "plant_4", "plant_4", "plant_4", "plant_4",
"plant_5", "plant_5", "plant_5", "plant_5", "plant_5"),
sp_rich = c(1, 1, NA, 1, NA,
1, 0, 0, NA, 0,
0, 1, 0, 0, 1,
0, NA, NA, 0,NA)),
row.names = c(NA, -20L), class = "data.frame",
.Names = c("plant_sp", "sp_rich"))
I'm trying to create an ifelse statemant using tidyverse by the following: for each group by the column "plant_sp" check if the value in column "sp_rich" is.na if it does, set value "1" in a new column called "is_na_nest_row". I managed to do the following:
test_df %>%
group_by(plant_sp) %>%
mutate(is_na_nest_row = ifelse(???,1,0))
but I don't know how to refer a value in column sp_rich
but in the next row (in a group)
For example:
I want the value "1" to be under is_na_nest_row
in row 3, if is there is empty row under sp_rich
at row 4.
Thanks a lot, Ido