[R] Loops to assign a unique ID to a column

David L Carlson dcarlson at tamu.edu
Tue Aug 2 17:36:10 CEST 2011


How about this?

> indx <- unique(cbind(Dates, Groups))
> indx
     Dates        Groups
[1,] "12/10/2010" "A"   
[2,] "12/10/2010" "B"   
[3,] "13/10/2010" "A"   
[4,] "13/10/2010" "B"   
[5,] "13/10/2010" "C" 

> indx <- data.frame(indx, id=1:nrow(indx))
> indx
       Dates Groups id
1 12/10/2010      A  1
2 12/10/2010      B  2
3 13/10/2010      A  3
4 13/10/2010      B  4
5 13/10/2010      C  5

> newdata <- merge(data, indx)
> newdata
       Dates Groups id
1 12/10/2010      A  1
2 12/10/2010      B  2
3 12/10/2010      B  2
4 13/10/2010      A  3
5 13/10/2010      B  4
6 13/10/2010      C  5

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Chandra Salgado Kent
Sent: Tuesday, August 02, 2011 2:12 AM
To: r-help at r-project.org
Subject: [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 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