如果时间跨度与小时重叠,则创建 24 列和一个虚拟列

Create 24 columns and a dummy if timespan overlaps with hour

我有一个 "start" 和 "end" 时间戳。

df <- structure(list(Vagt_Start = structure(c(1535412600, 1531006200, 
                                              1518823800, 1535671800, 1531092600, 1527550200, 1535499000, 1530919800, 
                                              1518910200, 1535585400), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                     Vagt_Slut = structure(c(1535441400, 1531035000, 1518852600, 
                                             1535700600, 1531121400, 1527579000, 1535527800, 1530948600, 
                                             1518939000, 1535614200), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .Names = c("start", 
                                                                                                                                  "end"), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
                                                                                                                                                                                   "data.frame"))

我想创建 24 个新专栏,并在那些时间添加。例如,如果第一行有 start=23:30 和 end=7:30,我想将它们放在第 23、00、01、...、07 列中。如何捕获所有 "hours"一个时期过去了。

一个包含 100 行的新数据集:

df <- structure(list(start = structure(c(1514818800, 1514793600, 1514764800, 
                                         1514791800, 1514793600, 1514764800, 1514764800, 1514793600, 1514793600, 
                                         1514793600, 1514847600, 1514764800, 1514764800, 1514764800, 1514793600, 
                                         1514764800, 1514790000, 1514764800, 1514764800, 1514794500, 1514793600, 
                                         1514793600, 1514764800, 1514793600, 1514793600, 1514793600, 1514764800, 
                                         1514764800, 1514793600, 1514793600, 1514764800, 1514791800, 1514764800, 
                                         1514764800, 1514764800, 1514794500, 1514793600, 1514793600, 1514793600, 
                                         1514818800, 1514764800, 1514793600, 1514793600, 1514793600, 1514764800, 
                                         1514793600, 1514764800, 1514764800, 1514764800, 1514793600, 1514793600, 
                                         1514764800, 1514764800, 1514764800, 1514820600, 1514764800, 1514764800, 
                                         1514764800, 1514792700, 1514764800, 1514793600, 1514793600, 1514793600, 
                                         1514764800, 1514793600, 1514793600, 1514764800, 1514793600, 1514793600, 
                                         1514764800, 1514797200, 1514793600, 1514764800, 1514793600, 1514791800, 
                                         1514764800, 1514793600, 1514764800, 1514764800, 1514764800, 1514793600, 
                                         1514764800, 1514764800, 1514793600, 1514764800, 1514791800, 1514764800, 
                                         1514764800, 1514764800, 1514793600, 1514764800, 1514764800, 1514793600, 
                                         1514793600, 1514764800, 1514764800, 1514793600, 1514764800, 1514764800, 
                                         1514793600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                     end = structure(c(1514847600, 1514820600, 1514851200, 1514819700, 
                                       1514820600, 1514851200, 1514851200, 1514820240, 1514820600, 
                                       1514817000, 1514876400, 1514851200, 1514851200, 1514851200, 
                                       1514820600, 1514851200, 1514804400, 1514851200, 1514851200, 
                                       1514819700, 1514818800, 1514820600, 1514851200, 1514820600, 
                                       1514820600, 1514820600, 1514851200, 1514851200, 1514820600, 
                                       1514820240, 1514851200, 1514818800, 1514851200, 1514851200, 
                                       1514851200, 1514821500, 1514820600, 1514820240, 1514820240, 
                                       1514847600, 1514851200, 1514820600, 1514822400, 1514820600, 
                                       1514851200, 1514820600, 1514851200, 1514851200, 1514851200, 
                                       1514820600, 1514820600, 1514791800, 1514851200, 1514851200, 
                                       1514849400, 1514851200, 1514851200, 1514851200, 1514819340, 
                                       1514851200, 1514820240, 1514820600, 1514820600, 1514851200, 
                                       1514817000, 1514820240, 1514851200, 1514820600, 1514820240, 
                                       1514851200, 1514808000, 1514820600, 1514851200, 1514820600, 
                                       1514819700, 1514851200, 1514820240, 1514851200, 1514851200, 
                                       1514851200, 1514820600, 1514851200, 1514851200, 1514820600, 
                                       1514851200, 1514818800, 1514851200, 1514851200, 1514851200, 
                                       1514820600, 1514851200, 1514851200, 1514820600, 1514820600, 
                                       1514851200, 1514851200, 1514820600, 1514851200, 1514791800, 
                                       1514820600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                     startN = c(1514818800, 1514793600, 1514764800, 1514791800, 
                                1514793600, 1514764800, 1514764800, 1514793600, 1514793600, 
                                1514793600, 1514847600, 1514764800, 1514764800, 1514764800, 
                                1514793600, 1514764800, 1514790000, 1514764800, 1514764800, 
                                1514794500, 1514793600, 1514793600, 1514764800, 1514793600, 
                                1514793600, 1514793600, 1514764800, 1514764800, 1514793600, 
                                1514793600, 1514764800, 1514791800, 1514764800, 1514764800, 
                                1514764800, 1514794500, 1514793600, 1514793600, 1514793600, 
                                1514818800, 1514764800, 1514793600, 1514793600, 1514793600, 
                                1514764800, 1514793600, 1514764800, 1514764800, 1514764800, 
                                1514793600, 1514793600, 1514764800, 1514764800, 1514764800, 
                                1514820600, 1514764800, 1514764800, 1514764800, 1514792700, 
                                1514764800, 1514793600, 1514793600, 1514793600, 1514764800, 
                                1514793600, 1514793600, 1514764800, 1514793600, 1514793600, 
                                1514764800, 1514797200, 1514793600, 1514764800, 1514793600, 
                                1514791800, 1514764800, 1514793600, 1514764800, 1514764800, 
                                1514764800, 1514793600, 1514764800, 1514764800, 1514793600, 
                                1514764800, 1514791800, 1514764800, 1514764800, 1514764800, 
                                1514793600, 1514764800, 1514764800, 1514793600, 1514793600, 
                                1514764800, 1514764800, 1514793600, 1514764800, 1514764800, 
                                1514793600), endN = c(1514847600, 1514820600, 1514851200, 
                                                      1514819700, 1514820600, 1514851200, 1514851200, 1514820240, 
                                                      1514820600, 1514817000, 1514876400, 1514851200, 1514851200, 
                                                      1514851200, 1514820600, 1514851200, 1514804400, 1514851200, 
                                                      1514851200, 1514819700, 1514818800, 1514820600, 1514851200, 
                                                      1514820600, 1514820600, 1514820600, 1514851200, 1514851200, 
                                                      1514820600, 1514820240, 1514851200, 1514818800, 1514851200, 
                                                      1514851200, 1514851200, 1514821500, 1514820600, 1514820240, 
                                                      1514820240, 1514847600, 1514851200, 1514820600, 1514822400, 
                                                      1514820600, 1514851200, 1514820600, 1514851200, 1514851200, 
                                                      1514851200, 1514820600, 1514820600, 1514791800, 1514851200, 
                                                      1514851200, 1514849400, 1514851200, 1514851200, 1514851200, 
                                                      1514819340, 1514851200, 1514820240, 1514820600, 1514820600, 
                                                      1514851200, 1514817000, 1514820240, 1514851200, 1514820600, 
                                                      1514820240, 1514851200, 1514808000, 1514820600, 1514851200, 
                                                      1514820600, 1514819700, 1514851200, 1514820240, 1514851200, 
                                                      1514851200, 1514851200, 1514820600, 1514851200, 1514851200, 
                                                      1514820600, 1514851200, 1514818800, 1514851200, 1514851200, 
                                                      1514851200, 1514820600, 1514851200, 1514851200, 1514820600, 
                                                      1514820600, 1514851200, 1514851200, 1514820600, 1514851200, 
                                                      1514791800, 1514820600)), .Names = c("start", "end", "startN", 
                                                                                           "endN"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                            -100L))

我一直在研究一种解决方案。不是很花哨,但很管用

is.wholenumber <- function(x, tol = .Machine$double.eps^0.5)  abs(x - round(x)) < tol
df <- structure(list(Vagt_Start = structure(c(1535412600, 1531006200, 
                                              1518823800, 1535671800, 1531092600, 1527550200, 1535499000, 1530919800, 
                                              1518910200, 1535585400), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                     Vagt_Slut = structure(c(1535441400, 1531035000, 1518852600, 
                                             1535700600, 1531121400, 1527579000, 1535527800, 1530948600, 
                                             1518939000, 1535614200), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                Vagt_Start2 = structure(c(1535412600, 1531006200, 
                                         1518823800, 1535671800, 1531092600, 1527550200, 1535499000, 1530919800, 
                                         1518910200, 1535585400)),
                Vagt_Slut2 = structure(c(1535441400, 1531035000, 1518852600, 
                                        1535700600, 1531121400, 1527579000, 1535527800, 1530948600, 
                                        1518939000, 1535614200))),.Names = c("start", "end", 'startN', 'endN'), 
                row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))

df$TimeS <- df$endN - df$startN
df$TimeM <- df$TimeS / 60
df$TimeH <- df$TimeM / 60
df$startingD <- substr(df$start, 1, 10)
df$endingD <- substr(df$end, 1, 10)
df$startingT <- as.numeric(substr(df$start, 12, 13)) + as.numeric(substr(df$start, 15, 16)) / 60
df$endingT <- as.numeric(substr(df$end, 12, 13)) + as.numeric(substr(df$end, 15, 16)) / 60
mat <- matrix(NA, nrow = nrow(df), ncol = 24)
colnames(mat) <- paste0('H', seq(1:24))
df <- cbind(df, mat)
for(i in 1:nrow(df)) {
  if (df$startingD[i] == df$endingD[i]) {
    vect <- seq(floor(df$startingT[i]), floor(df$endingT[i]))
    if (is.wholenumber(df$startingT[i]) & is.wholenumber(df$endingT[i])) {
      h_comp <- paste0('H', vect)
      h_nocomp <- NA
    } else if (is.wholenumber(df$startingT[i]) == FALSE & is.wholenumber(df$startingT[i]) == TRUE) {
      h_comp <- paste0('H', vect[-1])
      h_nocomp <- paste0(vect[1])
    } else if (is.wholenumber(df$startingT[i]) == TRUE & is.wholenumber(df$startingT[i]) == FALSE) {
      h_comp <- paste0('H', vect[-9])
      h_nocomp <- paste0('H', vect[9]     )
    } else if (is.wholenumber(df$startingT[i]) == FALSE & is.wholenumber(df$startingT[i]) == FALSE) {
      h_comp <- paste0('H', vect[c(-1, -9)])
      h_nocomp <- paste0('H', vect[c(1, 9)])  
    }
    for (j in 12:length(df)) {
      df[i, j] <- ifelse(names(df)[j] %in% h_comp, 1, ifelse(names(df)[j] %in% h_nocomp, 0.5, 0))
    }
  }
  else {
    vect <- c(seq(floor(df$startingT[i]), 24), seq(1, floor(df$endingT[i])))
    if (is.wholenumber(df$startingT[i]) & is.wholenumber(df$endingT[i])) {
      h_comp <- paste0('H', vect)
      h_nocomp <- NA
    } else if (is.wholenumber(df$startingT[i]) == FALSE & is.wholenumber(df$startingT[i]) == TRUE) {
      h_comp <- paste0('H', vect[-1])
      h_nocomp <- paste0('H', vect[1])
    } else if (is.wholenumber(df$startingT[i]) == TRUE & is.wholenumber(df$startingT[i]) == FALSE) {
      h_comp <- paste0('H', vect[-9])
      h_nocomp <- paste0('H', vect[9])     
    } else if (is.wholenumber(df$startingT[i]) == FALSE & is.wholenumber(df$startingT[i]) == FALSE) {
      h_comp <- paste0('H', vect[c(-1, -9)])
      h_nocomp <- paste0('H', vect[c(1, 9)])
      }
  }
  for (j in 12:length(df)) {
    df[i, j] <- ifelse(names(df)[j] %in% h_comp, 1, ifelse(names(df)[j] %in% h_nocomp, 0.5, 0))
  }
}

PS。我以为只能在半点或一点开始。