Dear Chandra, You're on the wrong track. You don't need for loops as you can do this vectorised.
as.numeric(interaction(data$Groups, data$Dates, drop = TRUE)) Best regards, Thierry > -----Oorspronkelijk bericht----- > Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] > Namens Chandra Salgado Kent > Verzonden: dinsdag 2 augustus 2011 9:12 > Aan: r-help@r-project.org > Onderwerp: [R] Loops to assign a unique ID to a column > > Dear R help, > > > > I am fairly new in data management and programming in R, and am trying to > write what is probably a simple loop, but am not having any luck. I have a > dataframe with something like the following (but much bigger): > > > > Dates<-c("12/10/2010","12/10/2010","12/10/2010","13/10/2010", > "13/10/2010", "13/10/2010") > > Groups<-c("A","B","B","A","B","C") > > data<-data.frame(Dates, Groups) > > > > I would like to create a new column in the dataframe, and give each distinct > date by group a unique identifying number starting with 1, so that the > resulting > column would look something like: > > > > ID<-c(1,2,2,3,4,5) > > > > The loop that I have started to write is something like this (but doesn't > work!): > > > > data$ID<-as.number(c()) > > for(i in unique(data$Dates)){ > > for(j in unique(data$Groups)){ data$ID[i,j]<-i > > i<-i+1 > > } > > } > > > > Am I on the right track? > > > > Any help on this is much appreciated! > > > > Chandra > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.