I would like to create a new frequency
column and fill it with the total count of each unique value in item
. I've tried:
df$frequency <- sum(df$item) #gives me total sum
df$frequency <- sum(unique(df$item)) # gives me 6 for some reason
df$frequency <- sum(df$item == 1) #gives me total count per selected value
But I would really like to generate them all at once.
example data:
> df <- data.frame("item" = c(1,1,1,1,2,2,2,3))
> df
item
1 1
2 1
3 1
4 1
5 2
6 2
7 2
8 3
desired output:
> df
item frequency
1 1 4
2 1 4
3 1 4
4 1 4
5 2 3
6 2 3
7 2 3
8 3 1
Much thanks in advance!