从字符转换日期时间为POSIXct对象

5
我有一个仪器,可以将数据导出为混乱的时间格式。我需要将datetime向量合并成一个新的datetime向量,并使用以下POSIXct格式:%Y-%m-%d %H:%M:%S。出于好奇心,我尝试了三种不同的方法来实现这一点,分别使用as.POSIXct()strftime()strptime()函数。当使用下面的示例数据时,只有as.POSIXct()strftime()函数能够正常工作,但我很想知道为什么strptime()会产生NAs?此外,我无法使用as.POSIXct()strftime()输出转换为POSIXct对象...

当在我的真实数据上尝试这些相同的函数(我只提供了前四行)时,我遇到了完全不同的问题。只有strftime()函数有效。由于某种原因,as.POSIXct()函数也会产生NAs,这是我实际需要用于将我的datetime转换为POSIXct对象的唯一命令...

似乎这些函数之间存在微妙的差别,我想知道如何更有效地使用它们。谢谢!

可重现示例:

## Creating dataframe:
date <- c("2017-04-14", "2017-04-14","2017-04-14","2017-04-14")
time <- c("14:24:24.992000","14:24:25.491000","14:24:26.005000","14:24:26.511000")
value <- c("4.106e-06","4.106e-06","4.106e-06","4.106e-06")
data <- data.frame(date, time)
data <- data.frame(data, value) ## I'm sure there is a better way to combine three vectors...
head(data)

## Creating 3 different datetime vectors:

## This works in my example code, but not with my real data... 
data$datetime1 <- as.POSIXct(paste(data$date, data$time), format = "%Y-%m-%d %H:%M:%S",tz="UTC")
class(data$datetime1)

## This is producing NAs, and I'm not sure why:
data$datetime2 <- strptime(paste(data$date, data$time), format = "%Y-%m-%d %H:%M%:%S", tz = "UTC") 
class(data$datetime2)

## This is working just fine
data$datetime3 <- strftime(paste(data$date, data$time), format = "%Y-%m-%d %H:%M%:%S", tz = "UTC")
class(data$datetime3)
head(data)

## Since I cannot get the as.POSIXct() function to work with my real data, I tried this workaround. Unfortunately I am running into trouble...
data$datetime4 <- as.POSIXct(x$datetime3, format = "%Y-%m-%d %H:%M%:%S", tz = "UTC")

真实数据链接这里

使用real_data.txt的示例:

## Reading in the file:
fpath <- "~/real_data.txt"
x <- read.csv(fpath, skip = 1, header = FALSE, sep = "", stringsAsFactors = FALSE)
names(x) <- c("date","time","bscat","scat_coef","pressure_mbar","temp_K","CH1","CH2") ## This is data from a Radiance Research Integrating Nephelometer Model M903 for anyone who is interested!

## If anyone could get this to work that would be awesome!
x$datetime1 <- as.POSIXct(paste(x$date, x$time), format = "%Y-%m-%d %H:%M%:%S", tz = "UTC") 

## This still doesn't work...
x$datetime2 <- strptime(paste(x$date, x$time), format = "%Y-%m-%d %H:%M%:%S", tz = "UTC") 

 ## This works: 
x$datetime3 <- strftime(paste(x$date, x$time), format = "%Y-%m-%d %H:%M%:%S", tz = "UTC")

## But I cannot convert from strftime character to POSIXct object, so it doesn't help me at all... 
x$datetime4 <- as.POSIXct(x$datetime3, format = "%Y-%m-%d %H:%M%:%S", tz = "UTC")    

head(x)
解决方案: 我没有为as.POSIXct()函数提供正确的格式字符串。一旦我将%Y-%m-%d %H:%M%:%S更改为%Y-%m-%d %H:%M:%Sdata$datetime2data$datetime4x$datetime1x$datetime2就正常工作了!非常感谢PhilC进行调试!
3个回答

13

如果您遇到真实数据的问题,请将%m%替换为%m

## Reading in the file:
fpath <- "c:/r/data/real_data.txt"
x <- read.csv(fpath, skip = 1, header = FALSE, sep = "", stringsAsFactors = FALSE)
names(x) <- c("date","time","bscat","scat_coef","pressure_mbar","temp_K","CH1","CH2") ## This is data from a Radiance Research Integrating Nephelometer Model M903 for anyone who is interested!

## issue was the %m% - fixed
x$datetime1 <- as.POSIXct(paste(x$date, x$time), format = "%Y-%m-%d %H:%M:%S", tz = "UTC") 

## Here too - fixed
x$datetime2 <- strptime(paste(x$date, x$time), format = "%Y-%m-%d %H:%M:%S", tz = "UTC") 
head(x)

1
谢谢!我的大脑玩了我!由于某种原因,我就是看不到它,但现在你指出来了,它是如此明显!哈哈,我想我现在要休息一下... - philiporlando

1

有一个格式字符串错误导致了 NAs;尝试这样做:

## This is no longer producing NAs:
data$datetime2 <- strptime(paste(data$date, data$time), format = "%Y-%m-%d %H:%M:%S",tz="UTC") 
class(data$datetime2)

哎呀,那是我的笔误,谢谢你发现了这个问题!我已经修正了我的例子。话虽如此,我使用的实际脚本格式是正确的,但仍然会产生NAs。 - philiporlando
我运行了已修正的版本,没有任何错误。函数调用中有一个隐藏字符导致了错误。如果您从答案处剪切/粘贴,则应该可以运行(对我而言确实如此)。PC - PhilC
哇!是啊,这对我有效!当你说隐藏的字符时,你的意思是我的原始脚本中有未显示的代码影响了我的日期时间转换吗?现在我只需要找到一种方法来让它与我的real_data.txt示例一起工作... - philiporlando

0

格式化为"%Y-%m-%d %H:%M:%OS"是一个通用视图。要将小数秒转换为特定位数的小数,请调用digits.sec选项,例如:

options(digits.secs=6) # This will take care of seconds up to 6 decimal points
data$datetime1 <- lubridate::parse_date_time(data$datetime, "%Y-%m-%d %H:%M:%OS")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接