This question already has an answer here:
-
Change variable value for the first row group_by subject ID using dplyr
2 answers
My df is as below:
set.seed(123)
df <- data.frame(x = sample(letters[1:3],20,replace = TRUE),
y = sample(1:10,20,replace = TRUE))
df <- df[order(df$x),]
I want to replace the first value of each group with NA. For example:
x y
a NA
a 8
a 1
a 8
b NA
b 3
b 2
b 10
b 8
.
.
I've no problem with get first values but that's not meaningful.
test <- df %>%
group_by(x) %>%
do(a = head(.$y,1))
Please help next step.
With dplyr
we could replace
values where row_number
is 1
library(dplyr)
df %>%
group_by(x) %>%
mutate(y = replace(y, row_number() == 1, NA))
# x y
# <fct> <int>
# 1 a NA
# 2 a 8
# 3 a 1
# 4 a 8
# 5 a 3
# 6 a 4
# 7 b NA
# 8 b 6
# 9 b 3
#10 b 2
#....
Or using base R ave
with(df, ave(y, x, FUN = function(i) replace(i, seq_along(i) == 1, NA)))
#[1] NA 8 1 8 3 4 NA 6 3 2 10 8 NA 10 7 6 10 7 5 3
With ifelse
and the use of a temp variable (pos
) that counts the position in the grouping:
library(dplyr)
df %>%
group_by(x) %>%
mutate(pos = 1:n(),
y=ifelse(pos==1, NA, y)) %>%
select(-pos) %>% # remove this to see what's happening in the ifelse()
head(8)
# # A tibble: 8 x 2
# # Groups: x [2]
# x y
# <fct> <int>
# 1 a NA
# 2 a 8
# 3 a 1
# 4 a 8
# 5 a 3
# 6 a 4
# 7 b NA
# 8 b 6