[R] Counts of duplicate rows as a new column without grouping of duplicates raws.

arun smartpink111 at yahoo.com
Thu Dec 26 17:48:40 CET 2013


Also, you could try:
library(plyr)
ddply(dat1,.(A),mutate, D=length(A))

#or
library(data.table)
 dt1 <- data.table(dat1,key='A')
 dt1[,D:=.N,by='A']
A.K.




On , arun <smartpink111 at yahoo.com> wrote:
Hi,
Try:
dat1 <- read.table(text="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",sep="",header=TRUE,stringsAsFactors=FALSE)

 within(dat1,D<-ave(seq_along(A),A,FUN=length))
A.K.





On Thursday, December 26, 2013 11:28 AM, Bartłomiej Taciak <bartek.taciak at gmail.com> wrote:
Hi everyone,

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

I don't want to simplify my data and grouping raws like in this thread:
https://stat.ethz.ch/pipermail/r-help/2011-March/270481.html

Thanks in advance for any help,
best regards,
Bartek

    [[alternative HTML version deleted]]

______________________________________________
R-help at 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.




More information about the R-help mailing list