Generating Variables to Number Observations Sequentially
For all observations and for observations within a group
To generate sequential numbers to identify your observations you can use the nrow function to create a sequence of numbers from 1 to the number of rows in your data frame.
When you have repeated data such as by id/date or hh/person you might want to number the observations within groups. To do this you will want to use the Table function to count the number of occurrences in each group. Then generate a sequence of numbers from 1 thru the Number of occurrences in each group to number your observations.
Look at the following code as an example (you can download the code to test it on your machine)
# Get data from url on this site and attach mydata <- read.csv("http://help.pop.psu.edu/help-by-software-package/r-project/datamgmt/visits.csv") attach(mydata) # get frequency table of the subjectid and sequence into the subjects visit number subjectvisit <- sequence(table(subjectid)) # get frequency table of the locationid and sequence to create a visit number for each location xlocationvisit <- sequence(table(locationid)) # number all obs visitnum <-c(1:nrow(mydata)) #create data frame containing new columns mynumbereddata <-cbind(mydata, visitnum,subjectvisit, xlocationvisit ) #look at xlocationvisit, need to sort to fix the problem mysorteddata<-sort(mynumbereddata, by=locationid) mysorteddata$locationvisit <- sequence(table(locationid))"Visitnum" can be used to re-sort your data to the original sort order.