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

-- 
Michael A. Miller                               [EMAIL PROTECTED]
  Imaging Sciences, Department of Radiology, IU School of Medicine


"ID","date","session"
1,05/24/2006,1
1,02/01/2007,2
1,05/23/2007,3
1,07/06/2007,4
2,07/28/2006,1
2,09/24/2006,2
2,01/18/2007,3
3,07/24/2006,1
3,01/17/2007,2
3,03/22/2007,3
4,05/08/2006,1
4,07/24/2006,2
4,09/26/2006,3
4,03/16/2007,4
5,07/19/2006,1
5,01/11/2007,2
5,05/04/2007,3
6,06/27/2006,1
6,08/15/2006,2
6,10/31/2006,3
6,02/27/2007,4
7,08/01/2006,1
7,10/06/2006,2
7,03/16/2007,3
8,06/06/2006,1
8,11/16/2006,2
8,04/24/2007,3
9,03/13/2007,1
9,04/27/2007,2
9,05/13/2007,3
10,08/03/2006,1
10,01/03/2007,2
10,04/25/2007,3
10,06/12/2007,4
11,05/24/2005,1
11,08/31/2006,2
11,04/10/2007,3
12,01/25/2007,1
12,04/30/2007,2
12,06/11/2007,3
1,05/24/2006,1
1,02/01/2007,2
1,05/23/2007,3
1,07/06/2007,4
2,07/28/2006,1
2,09/24/2006,2
2,01/18/2007,3
3,07/24/2006,1
3,01/17/2007,2
3,03/22/2007,3
4,05/08/2006,1
4,07/24/2006,2
4,09/26/2006,3
4,03/16/2007,4
5,07/19/2006,1
5,01/11/2007,2
5,05/04/2007,3
6,06/27/2006,1
6,08/15/2006,2
6,10/31/2006,3
6,02/27/2007,4
7,08/01/2006,1
7,10/06/2006,2
7,03/16/2007,3
8,06/06/2006,1
8,11/16/2006,2
8,04/24/2007,3
9,03/13/2007,1
9,04/27/2007,2
9,05/13/2007,3
10,08/03/2006,1
10,01/03/2007,2
10,04/25/2007,3
10,06/12/2007,4
11,05/24/2005,1
11,08/31/2006,2
11,04/10/2007,3
12,01/25/2007,1
12,04/30/2007,2
12,06/11/2007,3
1,05/24/2006,1
1,02/01/2007,2
1,05/23/2007,3
1,07/06/2007,4
2,07/28/2006,1
2,09/24/2006,2
2,01/18/2007,3
3,07/24/2006,1
3,01/17/2007,2
3,03/22/2007,3
4,05/08/2006,1
4,07/24/2006,2
4,09/26/2006,3
4,03/16/2007,4
5,07/19/2006,1
5,01/11/2007,2
5,05/04/2007,3
6,06/27/2006,1
6,08/15/2006,2
6,10/31/2006,3
6,02/27/2007,4
7,08/01/2006,1
7,10/06/2006,2
7,03/16/2007,3
8,06/06/2006,1
8,11/16/2006,2
8,04/24/2007,3
9,03/13/2007,1
9,04/27/2007,2
9,05/13/2007,3
10,08/03/2006,1
10,01/03/2007,2
10,04/25/2007,3
10,06/12/2007,4
11,05/24/2005,1
11,08/31/2006,2
11,04/10/2007,3
12,01/25/2007,1
12,04/30/2007,2
12,06/11/2007,3
1,05/24/2006,1
1,02/01/2007,2
1,05/23/2007,3
1,07/06/2007,4
2,07/28/2006,1
2,09/24/2006,2
2,01/18/2007,3
3,07/24/2006,1
3,01/17/2007,2
3,03/22/2007,3
4,05/08/2006,1
4,07/24/2006,2
4,09/26/2006,3
4,03/16/2007,4
5,07/19/2006,1
5,01/11/2007,2
5,05/04/2007,3
6,06/27/2006,1
6,08/15/2006,2
6,10/31/2006,3
6,02/27/2007,4
7,08/01/2006,1
7,10/06/2006,2
7,03/16/2007,3
8,06/06/2006,1
8,11/16/2006,2
8,04/24/2007,3
9,03/13/2007,1
9,04/27/2007,2
9,05/13/2007,3
10,08/03/2006,1
10,01/03/2007,2
10,04/25/2007,3
10,06/12/2007,4
11,05/24/2005,1
11,08/31/2006,2
11,04/10/2007,3
12,01/25/2007,1
12,04/30/2007,2
12,06/11/2007,3

______________________________________________
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.

Reply via email to