[R] Subset, period of days like one after another in a group

arun smartpink111 at yahoo.com
Tue May 20 19:02:32 CEST 2014



Hi,

In your example the "wea", only showed "dw".  Suppose the data is like this:

dat1 <- structure(list(date = structure(c(15765, 15766, 15767, 15768, 
15769, 15770, 15771, 15772, 15773, 15780, 15781, 15782, 15788, 
15789, 15790, 15791, 15792, 15795, 15796, 15797, 15798, 15799, 
15800, 15801, 15803, 15804, 15805, 15806), class = "Date"), mon = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Mrz", class = "factor"), 
    wea = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L), .Label = c("dw", "rw"), class = "factor")), .Names = c("date", 
"mon", "wea"), row.names = c("1156", "1157", "1158", "1159", 
"1160", "1161", "1162", "1163", "1164", "1171", "1172", "1173", 
"1179", "1180", "1181", "1182", "1183", "1186", "1187", "1188", 
"1189", "1190", "1191", "1192", "1193", "1194", "1195", "1196"
), class = "data.frame")


split(dat1,list(dat1$wea,cumsum(c(TRUE,diff(dat1$date)!=1))),drop=TRUE)
#or
 lapply(split(dat1,dat1$wea),function(x) split(x,cumsum(c(TRUE,diff(x$date)!=1))))
##if you want only "dw"
datdw <- droplevels(subset(dat1,wea=="dw"))  ##assuming that you wanted "wea" column to #be factor 
 split(datdw,cumsum(c(TRUE,diff(datdw$date)!=1)))
A.K.




On Tuesday, May 20, 2014 12:42 PM, arun <smartpink111 at yahoo.com> wrote:


Hi,
#if 'dat` is the dataset
May be this helps.
lst1 <- setNames(split(dat, cumsum(c(TRUE,diff(dat$date)!=1))),LETTERS[1:4])
A.K.




On Tuesday, May 20, 2014 12:17 PM, Christoph Schlächter <christoph.schlaechter at gmail.com> wrote:
Dear all,

I have a subset of a data frame with 3 columns and a few rows. The columns
are “date” [%Y %m %d], “mon” [%b]  and “wea” for type of weather with “dw”
dry weather “rw” rainy weather.

Here it is as a list:

structure(list(date = structure(c(15765, 15766, 15767, 15768,
15769, 15770, 15771, 15772, 15773, 15780, 15781, 15782, 15788,
15789, 15790, 15791, 15792, 15795), class = "Date"), mon = c("Mrz",
"Mrz", "Mrz", "Mrz", "Mrz", "Mrz", "Mrz", "Mrz", "Mrz", "Mrz",
"Mrz", "Mrz", "Mrz", "Mrz", "Mrz", "Mrz", "Mrz", "Mrz"), wea =
structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("dw", "rw"), class = "factor")), .Names = c("date",
"mon", "wea"), row.names = c(1156L, 1157L, 1158L, 1159L, 1160L,
1161L, 1162L, 1163L, 1164L, 1171L, 1172L, 1173L, 1179L, 1180L,
1181L, 1182L, 1183L, 1186L), class = "data.frame")

I want to get a period of days when it is dry weather and the difference
between days should be not more than one day.

For this example I need something to get:

Maybe as Group:

A:

2013-03-01 Mrz  dw
2013-03-02 Mrz  dw
2013-03-03 Mrz  dw
2013-03-04 Mrz  dw
2013-03-05 Mrz  dw
2013-03-06 Mrz  dw
2013-03-07 Mrz  dw
2013-03-08 Mrz  dw
2013-03-09 Mrz  dw

B

2013-03-16 Mrz  dw
2013-03-17 Mrz  dw
2013-03-18 Mrz  dw

C

2013-03-24 Mrz  dw
2013-03-25 Mrz  dw
2013-03-26 Mrz  dw
2013-03-27 Mrz  dw
2013-03-28 Mrz  dw

D

2013-03-31 Mrz  dw

How can I achive this. I really have only very few ideas like cast() or
melt() or both but i have trouble with the date format.

Thanks in advance. <r-help at r-project.org> <r-help at r-project.org>

    [[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