[R] Data Manipulation, add frequency index

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Sat Nov 8 11:56:14 CET 2008


one way is with ave(), e.g.,

dat <- data.frame(name = rep(c("Mary", "Sam", "John"), c(3,2,4)))
dat$freq <- ave(seq_along(dat$name), dat$name, FUN = seq_along)
dat


I hope it helps.

Best,
Dimitris


jie feng wrote:
> Hi, there,
> 
> I have a simple data manipulation question for you. Thank you for your help!
> 
>  Suppose that I have this data about people appearing in a class
> 
> 
> Mary
> Mary
> Mary
> Sam
> Sam
> John
> John
> John
> John
> 
> Then I want to find out what exact time(s) the student appears at the
> moment such as
> 
>  Mary 1
> Mary 2
> Mary 3
> Sam 1
> Sam 2
> John 1
> John 2
> John 3
> John 4
> 
> the fifth row shows tha Sam show the second times at the that moment.
> 
> How can I manipulate the data in this way. Suppose that now I just have
> "name" variable and want to add a colume of frequency?
> 
> Best
> 
> Jie
> 
> 	[[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.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-help mailing list