[R] Data Manipulation, add frequency index

Gabor Grothendieck ggrothendieck at gmail.com
Sat Nov 8 11:57:21 CET 2008


See ?ave and ?seq_along

DF <- data.frame(Name = c("Mary", "Mary", "Mary", "Sam", "Sam",
   "John", "John", "John", "John"), stringsAsFactors = FALSE)
DF$index <- ave(1:nrow(DF), DF$Name, FUN = seq_along)


On Sat, Nov 8, 2008 at 5:43 AM, jie feng <jiefeng222 at gmail.com> 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.
>



More information about the R-help mailing list