1

I have a column of ordered values in a data frame, what I need to do is create a new column with the number of times this value has occured.

Heres an example of my data:

5561
5561
5681
5681
5521
5521
5521
5521
5521

What I need

5561 1
5561 2
5681 1
5681 2
5521 1
5521 2
5521 3
5521 4
5521 5
CMAHER
  • 35
  • 5

1 Answers1

1
library(dplyr)

dat <- data.frame(v1 = c(5561, 5561, 5681, 5681, 5521, 5521, 5521, 5521, 5521))

dat %>%
    group_by(v1) %>%
    mutate(position = row_number())

# # A tibble: 9 x 2
# # Groups:   v1 [3]
# v1 position
# <dbl>    <int>
# 1  5561        1
# 2  5561        2
# 3  5681        1
# 4  5681        2
# 5  5521        1
# 6  5521        2
# 7  5521        3
# 8  5521        4
# 9  5521        5
heds1
  • 3,203
  • 2
  • 17
  • 32
  • Hi, the answer does exactly what you are asking. I would just say that the result you are looking is not transparent when you operate with larger dataset. You need to go to the last occurrences of unique value to see its frequency. You can also do that: dat <- data.frame(v1 = c(5561, 5561, 5681, 5681, 5521, 5521, 5521, 5521, 5521)) dat %>% group_by(v1) %>% summarize(count=n()) I have used @heds1 data... – Bloxx Jul 29 '21 at 10:40
  • `n()` just gives you the number in each group, it's not what OP is asking for. – heds1 Jul 29 '21 at 21:22
  • Yes, you are right. I wrote that in the case of huge datasets, this would not be optimal. Only the last row of each unique value gives you any information. In his desired output, it is obvious that before line #9, 5521 there would occur exactly 4 times. But, in line #7 you don't know it will occur 2 times more. My comment was not implying that your solution doesn't work, but simply advice for a more appropriate solution. – Bloxx Jul 30 '21 at 05:00