我有一个带有时间信息的向量,每天只记录一次日期。我需要将矢量转换为可用的格式,例如POSIXlt
。订购时间,一天内的所有时间(%H:%M
)都属于无日期时间之前的最后一个日期。
t <- structure(c(6L, 1L, 2L, 3L, 4L, 5L, 10L, 7L, 8L, 9L),
.Label = c("00:15", "00:25", "00:35", "00:45", "02:05", "20.01.2013; 0:05",
"20:48", "20:58", "21:08", "25.01.2013; 20:38"), class = "factor")
从多个先前的答案到有关因素转换的问题(例如here),我知道如何转换t[c(1, 7)]
。
t1 <- strptime(as.character(t[c(1, 7)]), format = "%d.%m.%Y; %H:%M")
# t1
# [1] "2013-01-20 00:05:00 CET" "2013-01-25 20:38:00 CET"
但是,如何传播剩余值的缺失日期以便它们正确转换?
答案 0 :(得分:2)
library(zoo) # For the na.locf function
df = data.frame(date=t)
# Put date and time in separate columns
df$time = gsub(".*; (.*)","\\1", df$date)
df$date = as.Date(df$date, format="%d.%m.%Y")
# Fill missing df$date values
df$date = na.locf(df$date)
# Convert to POSIXct
df$date = as.POSIXct(paste(df$date, df$time))
df = df[,1, drop=FALSE]
df
date
1 2013-01-20 00:05:00
2 2013-01-20 00:15:00
3 2013-01-20 00:25:00
4 2013-01-20 00:35:00
5 2013-01-20 00:45:00
6 2013-01-20 02:05:00
7 2013-01-25 20:38:00
8 2013-01-25 20:48:00
9 2013-01-25 20:58:00
10 2013-01-25 21:08:00
答案 1 :(得分:1)
我们可以使用dplyr
library(dplyr)
data.frame(t) %>%
mutate(Date = as.Date(t, "%d.%m.%Y")) %>%
group_by(grp = cumsum(!is.na(Date))) %>%
mutate(Date = Date[1L],
DateTime = as.POSIXct(paste(Date, sub(".*;", "", t)))) %>%
ungroup() %>%
select(DateTime)
# DateTime
# <dttm>
#1 2013-01-20 00:05:00
#2 2013-01-20 00:15:00
#3 2013-01-20 00:25:00
#4 2013-01-20 00:35:00
#5 2013-01-20 00:45:00
#6 2013-01-20 02:05:00
#7 2013-01-25 20:38:00
#8 2013-01-25 20:48:00
#9 2013-01-25 20:58:00
#10 2013-01-25 21:08:00
或使用base R
i1 <- nchar(as.character(t))==5
v1 <- ifelse(i1, paste(sub(";.*", ";", t[!i1])[cumsum(!i1)],
sub(".*;\\s+", "", t[i1])), as.character(t))
strptime(v1, "%d.%m.%Y %H:%M")