Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...