在给定时间间隔内使用 ifelse() 语句进行日期时间观察
Using an ifelse() statement for datetime observations within a given interval
我想根据 df$date
内的特定时间间隔为 df$lamp_intensity
向量分配一个值。如果给定的观察值超出此区间,我想分配一个 NA
。一旦我的第一段代码开始工作,我计划嵌套一堆 ifelse()
语句来处理多个时间间隔。我想我已经很接近了,但我绝对可以用一只手。
谢谢!
这是我的数据:
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)
该解决方案将为 2017-09-07 12:23:51
和 2017-09-07 12:23:55
之间的 df$lamp_intensity
行分配 0
您可以改用 cut
函数。例如:
df$lamp_intensity = cut(df$date,
breaks=as.POSIXct(c("2017-09-07 12:23:42","2017-09-07 12:23:55",
"2017-09-07 12:24:02", "2017-09-07 12:24:31"), tz="UTC"),
labels=c(0,1,2))
date ppm lamp_intensity
1 2017-09-07 12:23:48 0.0009765625 0
2 2017-09-07 12:23:50 0.0009765625 0
3 2017-09-07 12:23:51 0.0009765625 0
4 2017-09-07 12:23:52 0.0009765625 0
5 2017-09-07 12:23:53 0.0009765625 0
6 2017-09-07 12:23:54 0.0009765625 0
7 2017-09-07 12:23:55 0.0009765625 1
8 2017-09-07 12:23:56 0.0009765625 1
9 2017-09-07 12:23:57 0.0009765625 1
10 2017-09-07 12:23:58 0.0014648438 1
看起来很简单。
start <- as.POSIXct("2017-09-07 12:23:51", format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
end <- as.POSIXct("2017-09-07 12:23:55", format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
df$lamp_intensity <- ifelse(start <= df$date & df$date <= end, 0, NA)
我想根据 df$date
内的特定时间间隔为 df$lamp_intensity
向量分配一个值。如果给定的观察值超出此区间,我想分配一个 NA
。一旦我的第一段代码开始工作,我计划嵌套一堆 ifelse()
语句来处理多个时间间隔。我想我已经很接近了,但我绝对可以用一只手。
谢谢!
这是我的数据:
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)
该解决方案将为 2017-09-07 12:23:51
和 2017-09-07 12:23:55
df$lamp_intensity
行分配 0
您可以改用 cut
函数。例如:
df$lamp_intensity = cut(df$date,
breaks=as.POSIXct(c("2017-09-07 12:23:42","2017-09-07 12:23:55",
"2017-09-07 12:24:02", "2017-09-07 12:24:31"), tz="UTC"),
labels=c(0,1,2))
date ppm lamp_intensity
1 2017-09-07 12:23:48 0.0009765625 0
2 2017-09-07 12:23:50 0.0009765625 0
3 2017-09-07 12:23:51 0.0009765625 0
4 2017-09-07 12:23:52 0.0009765625 0
5 2017-09-07 12:23:53 0.0009765625 0
6 2017-09-07 12:23:54 0.0009765625 0
7 2017-09-07 12:23:55 0.0009765625 1
8 2017-09-07 12:23:56 0.0009765625 1
9 2017-09-07 12:23:57 0.0009765625 1
10 2017-09-07 12:23:58 0.0014648438 1
看起来很简单。
start <- as.POSIXct("2017-09-07 12:23:51", format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
end <- as.POSIXct("2017-09-07 12:23:55", format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
df$lamp_intensity <- ifelse(start <= df$date & df$date <= end, 0, NA)