I am trying to use group_by() to assign NA values only to the second row of a column of the grouped data using a conditional on the corresponding value in another column. The data and my attempt below. Thanks!
dat <- dat %>%
group_by(id, day) %>%
group_by(row_number() == 2) %>%
dplyr::mutate(newValue = ifelse(timeSinceWaking >= 0.5, value, NA)) %>%
ungroup()
id,day,timeSinceWaking,value
1,0,0,3
1,0,0.4,6
1,0,5,4
1,0,10,7
1,0,14,7
1,1,0,2
1,1,0.5,8
1,1,5,5
1,1,10,9
1,1,14,1
2,0,0,3
2,0,0.5,5
2,0,5,3
2,0,10,7
2,0,14,8
2,1,0,8
2,1,0.5,1
2,1,5,2
2,1,10,6
2,1,14,4