Skip to content
Back to formatted view

Raw Message

Message-ID: <50A33C0A.6020603@xtra.co.nz>
Date: 2012-11-14T06:36:58Z
From: Rolf Turner
Subject: numbering observations: help please!
In-Reply-To: <1352857803882-4649457.post@n4.nabble.com>

On 14/11/12 14:50, Paul Artes wrote:
> Dear Friends,
>
> I have the very simple problem of needing to number observations in a data
> frame. After scratching the rest of my hair off my head without inspiration,
> I'm using a silly loop. I'm sure that there is a much more elegant and
> faster solution - can anyone help?
>
> Here is an example:
>
> my.data <- data.frame (person=c(1,1,1,1,2,2,3,3,3,3,3,3,3,4,4,4))   # now I
> want to number those observations sequentially
> # for each person
>
> my.data$item.number <- 0
> for (i in 1:length(unique(my.data$person))) {
> 	my.data$item.number [which (my.data$person == unique(my.data$person)[i])]
> <- seq (1:dim(tmp)[1]) }
>
my.data$item.number <- 
with(my.data,unlist(tapply(1:length(person),person,function(x){1:length(x)})))

     cheers,

         Rolf Turner