Is goup_by() row_number() possible?

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

You are close. As you want to change an entry based on two attributes, you want that in the selection for what to change, in this case the ifelse

2nd is indeed relative to the group, so there needs to be a grouping on that defines the 2nd entry

i2 <- iris %>% arrange(Species, Sepal.Width) %>%
  group_by(Species) %>%
  mutate(added_value = ifelse(row_number() == 2 & Species == "versicolor", 0.5, 0))
1 Like

Thank you for your explanation and solution @thoughtfulnz! This is the code I ended up using:

dat <- dat %>%
    group_by(id, day) %>%
    arrange(id, day, timeSinceWaking) %>%
    dplyr::mutate(newValue = ifelse(row_number() == 2 & timeSinceWaking < 0.5, NA, value)) %>%
    ungroup()

I will just add as an addendum, that because of the changing relationship between arrange and group_by over the past few years, see:

https://github.com/tidyverse/dplyr/issues/1206

I tend to defensively apply a detailed arrange (including what I am going to group with) to have the entire dataset in the order I want, then apply the group_by to conduct group level transformations, then ungroup afterwards.

1 Like