Reputation: 16697
Trying to find dplyr
canonical way to mutate
a tibble
by row number.
I went through many Stack Overflow questions and GH issues, functions like row_number
, if_else
, case_when
, but it is still not clear what is the proper way to achieve the following.
Minimal example: having a data.frame, I would like to update one of its columns to a particular value (here NA
) for particular rows by providing row indices (here rows 2 and 4).
Column to update doesn't have to be parametrized, but only row numbers.
Below base R to achieve the following.
DF = data.frame(x=5:1)
idx = c(2L, 4L)
DF[idx, "x"] = NA_integer_
DF
# x
#1 5
#2 NA
#3 3
#4 NA
#5 1
Upvotes: 1
Views: 4351
Reputation: 3414
Based on @camille feedback and the consent by other commentors the most tidyverse-like code is mutate(x = ifelse(row_number() %in% idx, NA_integer_, x))
.
BTW. It is better to use <-
instead of =
assignments, and avoid usage capital letters in the names of objects. For more information please see The tidyverse style guide
So the full code:
library(dplyr)
df <- data.frame(x = 5:1)
idx <- c(2L, 4L)
df %>% mutate(x = ifelse(row_number() %in% idx, NA_integer_, x))
# x
#1 5
#2 NA
#3 3
#4 NA
#5 1
Upvotes: 2