[R] Group all the consecutive days

arun smartpink111 at yahoo.com
Wed Oct 2 17:45:41 CEST 2013


Hi,
Try:


dat1<- read.table(text='"year" "month" "day" "rainfall" "landslide"
"3" 2007 6 6 1.6 0
"4" 2007 6 7 1.8 0
"6" 2007 6 12 4.6 0
"8" 2007 7 5 6.6 0
"9" 2007 7 10 3 0
"10" 2007 7 11 1.2 0
"11" 2007 8 3 6.4 0
"12" 2007 8 10 2.8 0
"14" 2007 9 4 5.4 0
"15" 2007 9 5 1 0
"16" 2007 9 10 2.8 0
"17" 2007 9 11 6.8 0
"18" 2007 9 18 1.4 0
"19" 2007 9 19 1 0
"20" 2007 9 27 3 0
"21" 2007 10 6 41.4 0
"22" 2007 10 7 146 1
"23" 2007 10 10 2 0
"24" 2007 10 11 3.4 0
"26" 2007 10 18 17.4 0
"28" 2007 10 20 12.8 0
"29" 2007 10 21 1.8 0
"30" 2007 10 22 15.6 0
"33" 2007 10 25 8.6 0
"35" 2007 10 30 5.2 0
"36" 2007 10 31 34 1
"37" 2007 11 1 7.6 0
"39" 2007 11 9 6.8 1
"40" 2007 11 14 6.2 0
"41" 2007 11 15 3.8 0
"42" 2007 11 16 9.2 0',sep="",header=TRUE,stringsAsFactors=FALSE)
 vec1<- as.Date(paste(dat1[,1],sprintf("%02d",dat1[,2]), sprintf("%02d",dat1[,3]),sep="-"))
 indx<-c(0,cumsum(1-(diff(vec1)==1)))
res<- do.call(rbind,lapply(split(dat1,indx),function(x) {x$rainfall<-sum(x$rainfall);x$Date<-paste(paste(x[,1],sprintf("%02d",x[,2]),sprintf("%02d",x[,3]),sep="-"),collapse=",");x; x[1,c(6,4:5)]}))


A.K.



----- Original Message -----
From: Stefano Sofia <stefano.sofia at regione.marche.it>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Wednesday, October 2, 2013 10:09 AM
Subject: [R] Group all the consecutive days

Dear R-users,
I have a data frame where in each row there is the daily rainfall cumulative;
missing days mean that in that days rainfall has been zero.
I need to group all the consecutive days in a single row and store in the field "rainfall" the sum of these consecutive days.
Is there a reasonable way to do that?

Thank you for your help
Stefano

"year" "month" "day" "rainfall" "landslide"
"3" 2007 6 6 1.6 0
"4" 2007 6 7 1.8 0
"6" 2007 6 12 4.6 0
"8" 2007 7 5 6.6 0
"9" 2007 7 10 3 0
"10" 2007 7 11 1.2 0
"11" 2007 8 3 6.4 0
"12" 2007 8 10 2.8 0
"14" 2007 9 4 5.4 0
"15" 2007 9 5 1 0
"16" 2007 9 10 2.8 0
"17" 2007 9 11 6.8 0
"18" 2007 9 18 1.4 0
"19" 2007 9 19 1 0
"20" 2007 9 27 3 0
"21" 2007 10 6 41.4 0
"22" 2007 10 7 146 1
"23" 2007 10 10 2 0
"24" 2007 10 11 3.4 0
"26" 2007 10 18 17.4 0
"28" 2007 10 20 12.8 0
"29" 2007 10 21 1.8 0
"30" 2007 10 22 15.6 0
"33" 2007 10 25 8.6 0
"35" 2007 10 30 5.2 0
"36" 2007 10 31 34 1
"37" 2007 11 1 7.6 0
"39" 2007 11 9 6.8 1
"40" 2007 11 14 6.2 0
"41" 2007 11 15 3.8 0
"42" 2007 11 16 9.2 0


________________________________

AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere informazioni confidenziali, pertanto è destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si è il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell'art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed urgenza, la risposta al presente messaggio di posta elettronica può essere visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system.

    [[alternative HTML version deleted]]

______________________________________________
R-help at 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.




More information about the R-help mailing list