Skip to content
Prev 132550 / 398506 Next

creating a factor from dates by subject?

Dear R-help,

I have a data set consisting of measurements made on multiple
subjects.  Measurement sessions are repeated for each subject on
multiple dates.  Not all subjects have the same number of
sessions.  To create a factor that represents the session, I do
the following:

data <- read.csv('test-data.csv') # data appended below
data$date <- as.Date(data$date, format='%m/%d/%Y')
data$session <- rep(NA,nrow(data))
for (i in unique(data$ID)) {
  data$session[data$ID==i] <- as.numeric(factor(data$date[data$ID==i]))
}
data$session <- factor(data$session)

This results in a session column in the data frame that runs from
1 to the number of sessions for each subject ID. 

What do you R gurus think of this?  Is there a better more R-ish
way to do this with without creating the session variable in the
data frame and then looping?  I find myself doing this sort of
thing all the time and it feels crufty to me.  

Thanks, Mike