[R] Keep only first date from consecutive dates

Frank S. f_j_rod at hotmail.com
Fri Dec 4 21:53:49 CET 2015


Dear R users,
 
I usually work with data.table package, but I'm sure that muy question can also be answered working with R data frame.
Working with grouped data (by "id"),  I wonder if it is possible to keep in a R data.frame (or R data.table):
a) Only the first row if there is a row which belongs to a a group of rows (from same "id") that have consecutive dates.
b) All the rows which do not belong to the above groups.
 
As an example, I have "uci" data.frame:
 
uci <- data.table(id=c(rep(1,6),2),
                date = as.Date(c("2005-10-28","2005-10-29","2005-10-30","2005-11-07","2007-03-19","2007-03-20","2004-06-02")),
                value = c(1, 2, 1, 3, 1, 2, 2))
 
   id              date   value
    1  2005-10-28        1
    1  2005-10-29        2
    1  2005-10-30        1
    1  2005-11-07        3
    1  2007-03-19        1
    1  2007-03-20        2
    2  2004-06-02        2
 
And the desired output would be:
 
   id              date   value
    1  2005-10-28        1
    1  2005-11-07        3
    1  2007-03-19        1
    2  2004-06-02        2
 
# From the following link, I have tried:
http://stackoverflow.com/questions/32308636/r-how-to-sum-values-from-rows-only-if-the-key-value-is-the-same-and-also-if-the
 
setDT(uci)[ ,list(date=date[1L], value = value[1L]),  by = .(ind=rleid(date), id)][, ind:=NULL][]
 
But I get the same data frame, and I do not know the reason.
 
Thank you very much for any help!!
 
Frank S.
 
 
 
 
 		 	   		  
	[[alternative HTML version deleted]]



More information about the R-help mailing list