Skip to content

creating a factor from dates by subject?

2 messages · Michael A. Miller, PIKAL Petr

#
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
#
r-help-bounces at r-project.org napsal dne 20.12.2007 16:33:05:
If you do not heve identical dates in one ID then

unlist(lapply(rle(test$V1)$lengths, seq))

shall give you session vector.

Regards
Petr
http://www.R-project.org/posting-guide.html