r - 在 R 中将格式从数字更改为 POSIXct 对象
问题描述
我有一列包含这些日期和时间:
20121029 0,
20121029 100,
20121029 200,
20121029 300,
20121029 400 ...
它们的格式是“int”
我的任务是更改它们并以 POSIXct 格式创建一个表示时间和日期的列。
解决方案
这不是最优雅的解决方案,但它有效
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")
推荐阅读
- mysql - 将中值计算从 MySQL 转换为 SQL Server
- python - NameError:名称'zombie_array'未定义
- python - python 3 Tkinter ComboBox没有价值
- ruby-on-rails - Rubymine 在 Rails 上调试 Ruby 时出错
- python - 是否可以复制一个矩形并且两者都在不同的位置?
- c++ - 将一串随机的字符和整数序列解析为单独的向量
- c# - 在全屏模式下,C# 在 vlc 播放器上覆盖图片框
- html - 如何在 Angular 5 中动态读取外部模板
- javascript - 如何让噩梦强行超时
- tfs - 如何在 TFS 流程模板中创建审批流程?