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

Converting datetime from character to POSIXct object

我有一个以不规则的时间格式导出数据的仪器。我需要将 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")

Link到真实数据here

示例使用 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 调试!

格式字符串错误导致 NA;试试这个:

## 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)

对于您的真实数据问题,将 %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)

格式化为“%Y-%m-%d %H:%M:%OS”是通用视图。要将小数秒设置为特定的小数位数,请调用 degits.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")