R一些时间戳变为NA
我的代码正在读取CSV文件,并将时间戳列转换为R时间格式R一些时间戳变为NA,r,timestamp,R,Timestamp,我的代码正在读取CSV文件,并将时间戳列转换为R时间格式 DF <- read.csv("DF.CSV",head=TRUE,sep=",") DF[51082,1] [1] 03/01/2012 19:29 DF[1,1] [1] 02/24/12 00:29 DF尝试使用以下方法: > DF$START <- as.POSIXct(strptime(paste(DF$START),format="%m/%d/%Y %H:%M")) >DF$START尝试使用以下方
DF <- read.csv("DF.CSV",head=TRUE,sep=",")
DF[51082,1]
[1] 03/01/2012 19:29
DF[1,1]
[1] 02/24/12 00:29
DF尝试使用以下方法:
> DF$START <- as.POSIXct(strptime(paste(DF$START),format="%m/%d/%Y %H:%M"))
>DF$START尝试使用以下方法:
> DF$START <- as.POSIXct(strptime(paste(DF$START),format="%m/%d/%Y %H:%M"))
>DF$START您(至少)有两种不同的日期格式,
一个在%Y
(四位数年份)中,一个在%Y
(两位数年份)中。
除非12真的意味着12AD,否则你需要同时尝试这两种方法
DF <- data.frame(
START = c(
"03/01/2012 19:29",
"02/24/12 00:29"
),
stringsAsFactors = FALSE
)
coalesce <- function (x, ...) {
z <- class(x)
for (y in list(...)) {
x <- ifelse(is.na(x), y, x)
}
class(x) <- z
x
}
DF$START <- coalesce(
as.POSIXct(strptime(DF$START, format="%m/%d/%y %H:%M")),
as.POSIXct(strptime(DF$START, format="%m/%d/%Y %H:%M"))
)
# START
# 1 2012-03-01 19:29:00
# 2 2012-02-24 00:29:00
DF您(至少)有两种不同的日期格式,
一个在%Y
(四位数年份)中,一个在%Y
(两位数年份)中。
除非12真的意味着12AD,否则你需要同时尝试这两种方法
DF <- data.frame(
START = c(
"03/01/2012 19:29",
"02/24/12 00:29"
),
stringsAsFactors = FALSE
)
coalesce <- function (x, ...) {
z <- class(x)
for (y in list(...)) {
x <- ifelse(is.na(x), y, x)
}
class(x) <- z
x
}
DF$START <- coalesce(
as.POSIXct(strptime(DF$START, format="%m/%d/%y %H:%M")),
as.POSIXct(strptime(DF$START, format="%m/%d/%Y %H:%M"))
)
# START
# 1 2012-03-01 19:29:00
# 2 2012-02-24 00:29:00
DF