在 R 中的 posixlt 之间循环

Loop between posixlt in R

我在尝试随时间循环时在 R 中遇到错误。这是我的数据框的一个子集(包含 120000 行)。

                 time value      mean group
1 2017-01-01 12:00:00 0.507 0.5106533    NA
2 2017-01-01 12:05:00 0.526 0.5106533    NA
3 2017-01-01 12:10:00 0.489 0.5106533    NA
4 2017-01-01 12:15:00 0.598 0.5106533    NA
5 2017-01-01 12:20:00 0.564 0.5106533    NA
6 2017-01-01 12:25:00 0.536 0.5106533    NA

假设我想根据时间段创建组,预期结果如下:

                 time value      mean group
1 2017-01-01 12:00:00 0.507 0.5106533    A
2 2017-01-01 12:05:00 0.526 0.5106533    A
3 2017-01-01 12:10:00 0.489 0.5106533    B
4 2017-01-01 12:15:00 0.598 0.5106533    B
5 2017-01-01 12:20:00 0.564 0.5106533    C
6 2017-01-01 12:25:00 0.536 0.5106533    C

我尝试了以下代码:

for (i in 1:length(merged.data$group)){
  if (merged.data[as.POSIXlt(i)$time >= "2017-05-15 12:00:00 GMT" & 
as.POSIXlt(i)$time <= "2017-05-29 12:00:00 GMT",]){
   merged.data$group == "A"} 
  else if (merged.data[as.POSIXlt(i)$time >= "2017-08-11 12:00:00" & 
as.POSIXlt(i)$time <= "2017-11-29 16:00:00",]){
    merged.data$group == "B"}
  else if (merged.data[as.POSIXlt(i)$time >= "2018-01-05 12:00:00" & 
as.POSIXlt(i)$time <= "2018-02-16 16:00:00",]){
    merged.data$group == "C"}
}

我收到以下错误:

Error in as.POSIXlt.numeric(i) : 'origin' must be supplied

我不明白,我以为 POSIXlt 正在解决起源问题?虽然,我承认我对 R 中的时间问题的理解有点混乱,每次我需要处理 time/dates...

时我都很难编码

所以我希望有人能帮助我,如果我不清楚或者是否需要 more/better 信息来回答我的问题,请不要犹豫告诉我。

谢谢你提前 Whosebugers !

data.table方法...

示例数据

library( data.table )

dt <- fread("time value mean 
2017-01-01T12:00:00 0.507 0.5106533    
2017-01-01T12:05:00 0.526 0.5106533    
2017-01-01T12:10:00 0.489 0.5106533   
2017-01-01T12:15:00 0.598 0.5106533    
2017-01-01T12:20:00 0.564 0.5106533    
2017-01-01T12:25:00 0.536 0.5106533    ", header = TRUE)

dt[, time := as.POSIXct( time, format = "%Y-%m-%dT%H:%M:%S" )]

代码

library( data.table )
library( lubridate )

dt[, group := LETTERS[.GRP], by = lubridate::floor_date( time, "10 mins" ) ]

#             time value      mean group
# 1: 2017-01-01 12:00:00 0.507 0.5106533     A
# 2: 2017-01-01 12:05:00 0.526 0.5106533     A
# 3: 2017-01-01 12:10:00 0.489 0.5106533     B
# 4: 2017-01-01 12:15:00 0.598 0.5106533     B
# 5: 2017-01-01 12:20:00 0.564 0.5106533     C
# 6: 2017-01-01 12:25:00 0.536 0.5106533     C

更新

使用 foverlaps 的方法,基于提供的示例数据和代码

library( data.table )

#create lookup-table with periods and group-names  
periods.dt <- data.table( 
  start = as.POSIXct( c( "2017-05-15 12:00:00", "2017-08-11 12:00:00", "2018-01-05 12:00:00" ), tz = "GMT" ),
  stop = as.POSIXct( c( "2017-08-11 12:00:00", "2018-01-05 12:00:00", "2018-02-16 16:00:00"), tz = "GMT" ),
  group = LETTERS[1:3] )
#set keys
setkey( periods.dt, start, stop ) 

#create sample data
dt <- fread("time value mean 
            2017-01-01T12:00:00 0.507 0.5106533    
            2017-01-01T12:05:00 0.526 0.5106533    
            2017-01-01T12:10:00 0.489 0.5106533   
            2017-01-01T12:15:00 0.598 0.5106533    
            2017-01-01T12:20:00 0.564 0.5106533    
            2017-01-01T12:25:00 0.536 0.5106533    ", header = TRUE)

dt[, time := as.POSIXct( time, format = "%Y-%m-%dT%H:%M:%S", tz = "GMT" )]

#create dummies to join on
dt[, `:=`( start = time, stop = time )]

#perform overlap join, no match --> NA
foverlaps( dt, periods.dt, type = "within", nomatch = NA)[, c("time", "value","mean","group"), with = FALSE]
#                   time value      mean group
# 1: 2017-01-01 12:00:00 0.507 0.5106533  <NA>
# 2: 2017-01-01 12:05:00 0.526 0.5106533  <NA>
# 3: 2017-01-01 12:10:00 0.489 0.5106533  <NA>
# 4: 2017-01-01 12:15:00 0.598 0.5106533  <NA>
# 5: 2017-01-01 12:20:00 0.564 0.5106533  <NA>
# 6: 2017-01-01 12:25:00 0.536 0.5106533  <NA>

感谢您的回答,我发现只有日期对我有用,因为我的数据集存在巨大差距。通过一个简单的 ifelse,我发现了一些有用的东西:

merged.data$group<-ifelse(merged.data$date >= "2017-05-15" & merged.data$date <= "2017-05-29",1, ifelse(merged.data$date >= "2017-08-11" & merged.data$date <= "2017-11-29",2, ifelse(merged.data$date >= "2018-01-05" & merged.data$date <= "2018-02-16",3, NA )))

这不适用于我拥有的 POSIXlt 对象,但 Wimpel 提供的解决方案似乎有效(我在使用 data.table 时遇到问题,但那是另一回事了!)

再次感谢,这个论坛真的帮了大忙!