[R] Counting occurences of variables in a dataframe

David Winsemius dwinsemius at comcast.net
Sat Feb 11 22:05:25 CET 2012


On Feb 11, 2012, at 1:17 PM, Kai Mx wrote:

> Hi everybody,
> I have a large dataframe similar to this one:
> knames <-c('ab', 'aa', 'ac', 'ad', 'ab', 'ac', 'aa', 'ad','ae', 'af')
> kdate <- as.Date( c('20111001', '20111102', '20101001', '20100315',
> '20101201', '20110105', '20101001', '20110504', '20110603',  
> '20110201'),
> format="%Y%m%d")
> kdata <- data.frame (knames, kdate)

 >  ave(unclass(kdate), knames, FUN=order )
  [1] 2 2 1 1 1 2 1 2 1 1


That was actually not using the dataframe values but you could also do  
this:

 > kdata$ord <- with(kdata, ave(unclass(kdate), knames, FUN=order ))
 > kdata
    knames      kdate ord
1      ab 2011-10-01   2
2      aa 2011-11-02   2
3      ac 2010-10-01   1
4      ad 2010-03-15   1
5      ab 2010-12-01   1
6      ac 2011-01-05   2
7      aa 2010-10-01   1
8      ad 2011-05-04   2
9      ae 2011-06-03   1
10     af 2011-02-01   1

> I would like to add a new variable to the dataframe counting the
> occurrences of different values in knames in their order of appearance
> (according to the date as in indicated in kdate). The solution  
> should be a
> variable with the values 2,2,1,1,1,2,1,2,1,1. I could do it with a  
> loop,
> but there must be a more elegant way to this.
>
> Thanks!
>
> Best,
>
> Kai
>
> 	[[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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list