Suppose that  you have a dataframe myData, with columns A,B,C and you want
a new column D to have the counts for each item in column A, then try:

myData$D<-table(myData$A)[as.character(myData$A)]

The table creates the counts as a named vector by converting the column to
a factor, the names are the levels.
The result is indexed into the data set by level name, here the associated
character strings for myData$A.

Should work for integers, factors and character strings,
as.character is required for numeric values of myData$A

in response to:

My data looks like this one:
   A    B     C
1 a     1     ff
2 b     1     re
3 c     1     sd
5 a     2     as
6 c     4     fe
7 d     5      tt
8 d     5      tt
9 d     9      oi

I want to add a new column D, which will contain how many an element from
the column A is repeated, like this:
   A    B     C   D
1 a     1     ff    2
2 b     1     re  1
3 c     1     sd  2
5 a     2     as  2
6 c     4     fe   2
7 d     5     tt   3
8 d     5     tt   3
9 d     9     oi   3


Tom Jagger

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to