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]) } -- View this message in context: http://r.789695.n4.nabble.com/numbering-observations-help-please-tp4649457.html Sent from the R help mailing list archive at 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
On Tue, 13 Nov 2012, 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]) }Dunno what "tmp" is... but try this: my.data$item.number <- 1 my.data$item.number <- ave( my.data$item.number, my.data$person, FUN=cumsum ) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
HI, Try this: my.data$item_Num<- ave(my.data[,1],my.data[,1],FUN=seq_along) head(my.data) # ?person item_Num #1 ? ? ?1 ? ? ? ?1 #2 ? ? ?1 ? ? ? ?2 #3 ? ? ?1 ? ? ? ?3 #4 ? ? ?1 ? ? ? ?4 #5 ? ? ?2 ? ? ? ?1 #6 ? ? ?2 ? ? ? ?2 #or, unlist(tapply(my.data[,1],my.data[,1],FUN=seq_along),use.names=FALSE) # [1] 1 2 3 4 1 2 1 2 3 4 5 6 7 1 2 3 #or ?unlist(sapply(split(my.data,my.data[,1]),function(x) 1:nrow(x)),use.names=FALSE) # [1] 1 2 3 4 1 2 1 2 3 4 5 6 7 1 2 3 A.K. ----- Original Message ----- From: Paul Artes <paul_h_artes at yahoo.co.uk> To: r-help at r-project.org Cc: Sent: Tuesday, November 13, 2012 8:50 PM Subject: [R] numbering observations: help please! 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]) } -- View this message in context: http://r.789695.n4.nabble.com/numbering-observations-help-please-tp4649457.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.