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

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

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

chrstph
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Subset, period of days like one after another in a group

arun kirshna


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 <[hidden email]> 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. <[hidden email]> <[hidden email]>

    [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Subset, period of days like one after another in a group

arun kirshna


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 <[hidden email]> 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 <[hidden email]> 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. <[hidden email]> <[hidden email]>

    [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Subset, period of days like one after another in a group

jholtman
In reply to this post by chrstph
try this:

> x <- 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")
>
> # split into groups
> split(x, cumsum(c(TRUE, diff(x$date) > 1)))
$`1`
           date mon wea
1156 2013-03-01 Mrz  dw
1157 2013-03-02 Mrz  dw
1158 2013-03-03 Mrz  dw
1159 2013-03-04 Mrz  dw
1160 2013-03-05 Mrz  dw
1161 2013-03-06 Mrz  dw
1162 2013-03-07 Mrz  dw
1163 2013-03-08 Mrz  dw
1164 2013-03-09 Mrz  dw

$`2`
           date mon wea
1171 2013-03-16 Mrz  dw
1172 2013-03-17 Mrz  dw
1173 2013-03-18 Mrz  dw

$`3`
           date mon wea
1179 2013-03-24 Mrz  dw
1180 2013-03-25 Mrz  dw
1181 2013-03-26 Mrz  dw
1182 2013-03-27 Mrz  dw
1183 2013-03-28 Mrz  dw

$`4`
           date mon wea
1186 2013-03-31 Mrz  dw

>



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 Tue, May 20, 2014 at 12:16 PM, Christoph Schlächter <
[hidden email]> 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. <[hidden email]> <[hidden email]>
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] 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.
>
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.