我有一个“开始”和“结束”时间戳。
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个新列,并在那几个小时内放入。例如,如果第一行的开始时间是23:30,结束时间是7:30,我想在第23、00、01,...,07列中添加一个。如何捕获一个时期内的所有“小时”十字架。
具有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))
答案 0 :(得分:0)
我一直在一个解决方案中工作。它不是很花哨,但有效
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。我以为那只能从凌晨三点开始。