【问题标题】:Changing the format from numeric to POSIXct object in R在 R 中将格式从数字更改为 POSIXct 对象
【发布时间】:2019-02-09 01:49:27
【问题描述】:

我有一列包含这些日期和时间:

20121029 0,
20121029 100,
20121029 200,
20121029 300,
20121029 400 ...

它们的格式是“int”

我的任务是更改它们并以 POSIXct 格式创建一个表示时间和日期的列。

【问题讨论】:

  • 你的意思是你有两列,每列一个int?
  • 0, 100, 200 等是否代表一天中的时间(大概是 24 小时制,还是与日期/时间无关?
  • G5W 是的。 User5783745,是的,它代表 24 小时制的一天中的时间

标签: r posixct


【解决方案1】:

这不是最优雅的解决方案,但它确实有效

library(stringr)
library(dplyr)

example_data <- c("20121029 0", "20121029 100", "20121029 2100")

# Extract the times
times <- example_data %>% strsplit(. , " ") %>% sapply(. , "[", 2)
times <- str_pad(times, max(nchar(times)), side="right", pad="0")

# Extract just the dates
dates <- example_data %>% substr(., 1, 8)

# Combine date and time and convert to POSIXct
date_time <- paste(dates, times) %>% as.POSIXct(., format="%Y%m%d %H%M")

【讨论】:

  • 非常感谢!这对我帮助很大!
猜你喜欢
  • 2021-12-09
  • 1970-01-01
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多