How to compute first, second etc. time when observation occurs in df?

Hi, I have a following dataset:

df <- data.frame(year = c(2003,2003,2004,2002,2004,2004,2005), name = c("A","A","A","B","B","B","B" ) , 
                 value = c(5,7,1,10,6,8,15))

I need to compute new column "season", showing the number of season for each name. In other words, I want to have:

  year name value season
1 2003    A     5      1
2 2003    A     7      1
3 2004    A     1      2
4 2002    B    10     1
5 2004    B     6      2
6 2004    B     8      2
7 2005    B    15     3

Can anybody help me, please?

Here is one method.

df <- data.frame(year = c(2003,2003,2004,2002,2004,2004,2005), 
                 name = c("A","A","A","B","B","B","B" ) , 
                 value = c(5,7,1,10,6,8,15))
library(dplyr)
Seasons <- unique(df[, c("year", "name")]) %>% arrange(name, year) %>% 
  group_by(name) %>% 
  mutate(Season = row_number())
df <- inner_join(df, Seasons, by = c("name", "year"))
df  
#>   year name value Season
#> 1 2003    A     5      1
#> 2 2003    A     7      1
#> 3 2004    A     1      2
#> 4 2002    B    10      1
#> 5 2004    B     6      2
#> 6 2004    B     8      2
#> 7 2005    B    15      3

Created on 2020-03-25 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.