将datetime从character转换为POSIXct对象

时间:2017-04-17 22:01:47

标签: r datetime date-formatting posixct strptime

我有一种以不守规矩的时间格式导出数据的工具。我需要将datetime向量组合成以下datetime格式的新POSIXct向量:%Y-%m-%d %H:%M:%S。出于好奇,我尝试使用as.POSIXct()strftime()strptime()以三种不同的方式执行此操作。在下面使用我的示例数据时,只有as.POSIXct()strftime()函数有效,但我很好奇为什么strptime()正在生成NAs?另外,我无法使用strftime()POSIXct输出转换为as.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")

链接到真实数据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:%S后,data$datetime2data$datetime4x$datetime1x$datetime2工作正常!非常感谢PhilC进行调试!

3 个答案:

答案 0 :(得分:3)

对于您的真实数据问题,请将%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 :(得分:1)

导致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)

答案 2 :(得分:0)

格式化为“%Y-%m-%d%H:%M:%OS”是通用视图。要将小数秒设置为特定的小数位数,请调用degits.sec的选项,例如:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="450" height="400" version="1.1">
  <style type="text/css">
    <![CDATA[
      rect {fill:white; stroke: black; stroke-width: 1;}
      text {fill: black; font-family: sans-serif; font-size: 10pt}
      line {stroke:black; stroke-width:2}
    ]]>
  </style>
  <defs>
    <marker orient="auto" refY="0.0" refX="0.0" id="StartArrow" style="overflow:visible;">
      <path style="fill-rule:evenodd;stroke-width:0.625;stroke-linejoin:round;stroke:#000000;stroke-opacity:1;fill:#000000;fill-opacity:1" d="M 8.7185878,4.0337352 L -2.2072895,0.016013256 L 8.7185884,-4.0017078 C 6.9730900,-1.6296469 6.9831476,1.6157441 8.7185878,4.0337352 z " />
    </marker>
    <use id="eaExperiment" href="#StartArrow" transform="rotate(180)" />
    <marker orient="auto" refY="0.0" refX="0.0" id="EndArrow" style="overflow:visible;">
      <path style="fill-rule:evenodd;stroke-width:0.625;stroke-linejoin:round;stroke:#000000;stroke-opacity:1;fill:#000000;fill-opacity:1" d="M 8.7185878,4.0337352 L -2.2072895,0.016013256 L 8.7185884,-4.0017078 C 6.9730900,-1.6296469 6.9831476,1.6157441 8.7185878,4.0337352 z " transform="rotate(180)" />
    </marker>
    <svg id="Box">
      <rect width="100" height="85" x="1" y="1" />
      <text x="5" y="20">The box</text>
      <svg x="10" y="25">
        <rect width="70" height="50" x="1" y="1" />
        <text x="5" y="20">Box</text>
        <text x="5" y="40">Contents</text>
      </svg>
    </svg>
  </defs>
  <svg>
    <svg x="10" y="120">
      <rect width="100" height="50" x="1" y="1" />
      <text x="5" y="20">Data</text>
      <text x="5" y="40">source</text>
    </svg>
    <svg x="150">
      <use href="#Box" y="1" />
      <use href="#Box" y="100" />
      <use href="#Box" y="200" />
    </svg>
    <svg x="300" y="120">
      <rect width="100" height="50" x="1" y="1" />
      <text x="5" y="20">Database</text>
      <text x="5" y="40">server</text>
    </svg>
    <line x1="100" y1="120" x2="148" y2="40" style="marker-end:url(#EndArrow)" />
    <line x1="110" y1="150" x2="147" y2="150" style="marker-end:url(#EndArrow)" />
    <line x1="100" y1="170" x2="148" y2="240" style="marker-end:url(#EndArrow)" />
    <line x1="254" y1="40" x2="297" y2="120" style="marker-start:url(#StartArrow); marker-end:url(#EndArrow)" />
    <line x1="250" y1="150" x2="297" y2="150" style="marker-start:url(#eaExperiment); marker-end:url(#EndArrow)" />
    <line x1="250" y1="240" x2="297" y2="170" style="marker-end:url(#EndArrow)" />
  </svg>
</svg>