Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k views
in Technique[技术] by (71.8m points)

New column keeping first occurrence of another column in r

The data is as follows:

cat <- c('A','A','A','B','B','A','C','B')
value <- c(1,1,1,2,2,1,3,2)
df <- data.frame(cat,value)

I want to create a new column which keeps the first occurrence of each 'cat'.

Sample Output enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...