[R] extracting values conditonal on other values

Toby Gass tobygass at warnercnr.colostate.edu
Thu Mar 4 02:02:50 CET 2010


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



More information about the R-help mailing list