Order is important!
Base R
df$new <- ave(df$value, df$cat, FUN = function(z) c(z[1], rep(0, length(z)-1)))
df
# cat value new
# 1 A 1 1
# 2 A 1 0
# 3 A 1 0
# 4 B 2 2
# 5 B 2 0
# 6 A 1 0
# 7 C 3 3
# 8 B 2 0
dplyr
library(dplyr)
df %>%
group_by(cat) %>%
mutate(new = if_else(row_number() == 1, value, 0)) %>%
ungroup()
# # A tibble: 8 x 3
# cat value new
# <chr> <dbl> <dbl>
# 1 A 1 1
# 2 A 1 0
# 3 A 1 0
# 4 B 2 2
# 5 B 2 0
# 6 A 1 0
# 7 C 3 3
# 8 B 2 0
or
df %>%
group_by(cat) %>%
mutate(new = c(value[1], rep(0, n() - 1))) %>%
ungroup()
data.table
library(data.table)
DF <- as.data.table(df)
DF[, new := c(value[1], rep(0, .N - 1)), by = .(cat)]
# cat value new
# <char> <num> <num>
# 1: A 1 1
# 2: A 1 0
# 3: A 1 0
# 4: B 2 2
# 5: B 2 0
# 6: A 1 0
# 7: C 3 3
# 8: B 2 0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…