Suppose I have a data frame such like:
set.seed(123)
df<-data.frame(y=sample( c("A","B","C"), 10, T),
X=sample(c (1,2,3), 10, T))
y X
1 A 3
2 C 2
3 B 3
4 C 2
5 C 1
6 A 3
7 B 1
8 C 1
9 B 1
10 B 3
what I wanted is to add a column z
which summarize the items' length of column y
such as:
y X z
1 A 3 2
2 C 2 4
3 B 3 4
4 C 2 4
5 C 1 4
6 A 3 2
7 B 1 4
8 C 1 4
9 B 1 4
10 B 3 4
which means there are 2 As, 4 Cs and 4 Bs.