Re: [R] creating a count variable in R

2011-03-03 Thread Bill.Venables
You can probably simplify this if you can assume that the dates are in sorted order. Here is a way of doing it even if the days are in arbitrary order. The count refers to the number of times that this date has appeared so far in the sequence. con <- textConnection(" 01/01/2011 01/01/2011 02/

Re: [R] creating a count variable in R

2011-03-03 Thread David Winsemius
On Mar 3, 2011, at 3:58 PM, JonC wrote: Hi R helpers, I'm trying to create a count in R , but as there is no retain function like in SAS I'm running into difficulties. Your data is not cut-pastable as presented but this should work: > dfrm$count_var <- ave(as.numeric(dfrm$Date_var), dfrm

Re: [R] creating a count variable in R

2011-03-03 Thread William Dunlap
Use cumsum() to count the change points: > Date_var <- as.Date(rep(c("2011-02-04","2011-02-07","2011-01-29"), c(2,3,1))) > data.frame(Date_var, count=cumsum(c(TRUE, Date_var[-1]!=Date_var[-length(Date_var)]))) Date_var count 1 2011-02-04 1 2 2011-02-04 1 3 2011-02-07 2 4 2011-02-07