vendredi 8 septembre 2017

Using an ifelse() statement for datetime observations within a given interval

I would like to assign a value to the df$lamp_intensity vector depending on a specific time interval within df$date. If a given observation is outside of this interval, I would like to assign an NA. Once I get this first bit of code working, I plan on nesting a bunch of ifelse() statements to handle multiple time intervals. I think I'm pretty close, but I could definitely use a hand.

Thank you!

Here's my data:

df <- structure(list(date = structure(c(1504787028, 1504787030, 1504787031, 1504787032, 1504787033, 1504787034, 1504787035, 1504787036, 1504787037, 1504787038), class = c("POSIXct", "POSIXt"), tzone = "UTC"), ppm = c(0.0009765625, 0.0009765625, 0.0009765625, 0.0009765625, 0.0009765625, 0.0009765625, 0.0009765625, 0.0009765625, 0.0009765625, 0.00146484375)), .Names = c("date", "ppm"), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))

df$lamp_intensity <- ifelse(df$date >= as.POSIXct("2017-09-07 12:23:51",
    format = "%Y-%m-%d %H:%M:%S", tz = "UTC") && ## using '&' generates an error message
    date <= as.POSIXct("2017-09-07 12:23:55",
    format = "%Y-%m-%d %H:%M:%S", tz = "UTC"), 0, NA)

head(df, 10)

Aucun commentaire:

Enregistrer un commentaire