Home > front end >  Adding the first, second, third observation in a data frame
Adding the first, second, third observation in a data frame

Time:05-06

I am working with a dataset containing serial numbers. I would like to add a column that defines if it is the first, second or nth time that the serial number occurs. For example:

serial   occurrence
   110            1
   111            1
   112            1
   110            2
   110            3

CodePudding user response:

You can use the following code:

df <- data.frame(serial = c(110, 111, 112, 110, 110))

library(dplyr)

df %>% 
  group_by(serial) %>% 
  mutate(occurence=1:n())

Output:

# A tibble: 5 × 2
# Groups:   serial [3]
  serial occurence
   <dbl>     <int>
1    110         1
2    111         1
3    112         1
4    110         2
5    110         3

CodePudding user response:

A base solution with ave() and seq_along:

within(df, {
  occurence <- ave(serial, serial, FUN = seq_along)
})

#   serial occurence
# 1    110         1
# 2    111         1
# 3    112         1
# 4    110         2
# 5    110         3
  •  Tags:  
  • r
  • Related