[R] Creating a new factor from other factors and a date range

Brough, Tyler (FRS) TBrough at russell.com
Thu Feb 17 18:14:02 CET 2005


Hello useRs,

I'm using version 2.0.1 on Windows XP.  I have a data.frame with 3 factors
and a date. The data.frame is sorted by the 3 factors and by date.  I would
like to create a new factor designating membership in a group.  Each group
is defined as having the same factor values and dates that are less than
some number (e.g. 10) days apart.  Does anyone have any suggestions as to
how to accomplish this task without looping over my data?  I would like to
be able to possibly use more than three factors and any other number of
days. 

The following replicates my data:

set.seed(5)
my.df <- data.frame(f1 = as.factor(sample(1000:1010,size=100,replace=T)),
	f2 = as.factor(sample(c("A","B"),size=100,replace=T)),
	f3 = as.factor(sample(c(T,F),size=100,replace=T)),
	td = sample(seq(ISOdate(2004,1,1),ISOdate(2004,2,29),by="days"),
		 size = 100, replace = T) )

my.df <- my.df[order(my.df[,1],my.df[,2],my.df[,3],my.df[,4]), ]

So, for example looking at the first 5 rows of my.df gives:

> my.df[1:5,]
     f1 f2    f3                  td
35 1000  A FALSE 2004-01-28 12:00:00
77 1000  A FALSE 2004-02-16 12:00:00
32 1000  A  TRUE 2004-01-02 12:00:00
75 1000  A  TRUE 2004-01-22 12:00:00
86 1000  A  TRUE 2004-01-29 12:00:00


The first row would be assigned to group 1, row 2 to group 2, row 3 to group
3, and the next two rows to group 4 (using a 10 day date range).

Thank you in advance for your suggestions.

-Tyler




More information about the R-help mailing list