Search code examples
rdataframedplyrcol

add column to dataframes from 1 to unique length of existing grouped rows


Here is my example df:

df = read.table(text = 'colA 
22
22
22
45
45
11
11
87
90
110
32
32', header = TRUE)

I just need to add a new col based on colA with values from 1 to the unique length of colA.

Expected output:

   colA   newCol 
    22     1
    22     1
    22     1
    45     2
    45     2
    11     3
    11     3
    87     4
    90     5
    110    6 
    32     7
    32     7

Here is what I tried without succes:

library(dplyr)
new_df = df %>%
  group_by(colA) %>% 
  mutate(newCol = seq(1, length(unique(df$colA)), by = 1))

Thanks


Solution

  • newcol = c(1, 1+cumsum(diff(df$colA) != 0))
     [1] 1 1 1 2 2 3 3 4 5 6 7 7