[R] Group all the consecutive days
jim holtman
jholtman at gmail.com
Wed Oct 2 17:29:23 CEST 2013
try this:
> rain <- 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', header = TRUE)
>
> # convert to Date
> rain$Date <- as.Date(paste0(rain$year, '-', rain$month, '-', rain$day))
>
> # determine consecutive if difference is one
> rain$consec <- cumsum(!c(TRUE, diff(rain$Date) == 1))
>
> # now split by consecutive days and create one row
> x <- split(rain, rain$consec)
>
> result <- do.call(rbind
+ , lapply(x, function(days){
+ data.frame(date = paste(days$Date, collapse = ',')
+ , total = sum(days$rainfall)
+ , stringsAsFactors = FALSE
+ )
+ })
+ )
>
>
>
> result
date total
0 2007-06-06,2007-06-07 3.4
1 2007-06-12 4.6
2 2007-07-05 6.6
3 2007-07-10,2007-07-11 4.2
4 2007-08-03 6.4
5 2007-08-10 2.8
6 2007-09-04,2007-09-05 6.4
7 2007-09-10,2007-09-11 9.6
8 2007-09-18,2007-09-19 2.4
9 2007-09-27 3.0
10 2007-10-06,2007-10-07 187.4
11 2007-10-10,2007-10-11 5.4
12 2007-10-18 17.4
13 2007-10-20,2007-10-21,2007-10-22 30.2
14 2007-10-25 8.6
15 2007-10-30,2007-10-31,2007-11-01 46.8
16 2007-11-09 6.8
17 2007-11-14,2007-11-15,2007-11-16 19.2
>
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Wed, Oct 2, 2013 at 10:09 AM, Stefano Sofia
<stefano.sofia at regione.marche.it> wrote:
> 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