[R] extracting values conditonal on other values
Phil Spector
spector at stat.berkeley.edu
Thu Mar 4 02:28:25 CET 2010
Toby -
Thanks for the reproducible example!
I think this will do what you want:
both = merge(test1,test2)
subset(both,time >= rise & time <= set)
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Wed, 3 Mar 2010, Toby Gass wrote:
> Dear R helpers,
>
> I have a dataframe (test1) containing the time of sunrise and sunset for each day of the year
> for 3 years. I have another dataframe (test2) containing measurements that are taken every
> 15 minutes, 24/7. I would like to extract all rows from test2 that occur between sunrise and
> sunset for the appropriate date. Can you suggest a good vectorized way to do this? Keep in
> mind that the sunrise/sunset dataframe has 1 row for each day, and the measurement
> dataset has 96 rows for each day. I'm hoping not to have to match strings...
>
> The times (test1$rise, test1$set, and test2$time) in the example are rather ugly since I wasn't
> sure how to generate a random hourly time series. I would also use a standard date format
> for the real thing but, again, wasn't sure how to generate dates for this example.
>
> Example data:
>
> test1 <- data.frame(year = gl(3, 30, 90, labels = c("2006", "2007", "2008")),
> month = gl(3, 10, 30, labels = c("1", "2", "3")),
> day = rep(c(21:30, 19:28, 22:31),3),
> rise = as.integer(runif(90, 700, 750)),
> set = as.integer(runif(90, 1630,1745)))
>
>
> test2 <- data.frame(year = gl(3, 2880, 8640, labels = c("2006", "2007", "2008")),
> month = gl(3, 96, 288, labels = c("1", "2", "3")),
> day = rep(c(21:30, 19:28, 22:31),3, each = 96),
> time = 100*rep(seq(0000, 23.75,by= .25),90),
> temp = runif(8640, -5, 15))
>
> Thank you in advance,
>
> Toby
>
> ______________________________________________
> 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