r - 将字符数据转换为日期——COVID 19 研究
问题描述
需要帮助将字符数据转换为日期:
时间/日期如下所示:4/10/20 17:36
我需要将列从字符转换为日期,以便按日期对列进行排序。我的数据框是o2outcomes
,我的专栏是transfusion_date
我试过了:
o2outcomes$transfusion_date <- as.Date(c("2007-06-22", "2004-02-13"))
o2outcomes$transfusion_date <- as.Date(o2outcomes$transfusion_date , "%m/%d/%Y %H%M")
o2outcomes <- data.frame(Date = c("10/9/2009 0:00:00", "10/15/2009 0:00:00")) as.Date(o2outcomes$transfusion_date, "%m/%d/%Y %H:%M")
为了按日期排列数据,我尝试过:
o2outcomes$V8 <- lubridate::dmy(o2outcomes$transfusion_date) dplyr::arrange(o2outcomes$transfusion_date)
o2outcomes[order(as.Date(o2outcomes$transfusion_date, format="%d/%m/%Y")),]
test <- "2013-12-25T04:32:16.500-08:00
z <- as.POSIXct(test,format="%Y-%m-%dT%H:%M:%OS")
op <- options(digits.secs = 3)
z
'test <- "2013-12-25 04:32:16.5 CST"
as.POSIXct(test)
4.
o2outcomes$V8 <- lubridate::dmy(o2outcomes$transfusion_date)
dplyr::arrange(o2outcomes$transfusion_date)
5.
o2outcomes[order(as.Date(o2outcomes$transfusion_date, format="%d/%m/%Y")),]
这是一项对时间敏感的 COVID 研究(全国首个用于血浆疗法的研究!),尽管我以前从未使用过 R,但我的任务是创建这些数字。所以提前谢谢。
凯特
解决方案
让我们弥补一些数据。你不需要这样做,因为你已经有了你的数据。我想你可能有更多的列,这不是问题。
o2outcomes <- data.frame(transfusion_date= c("4/10/20 17:36", "4/10/20 17:33", "4/11/20 17:36"),
other = letters[1:3])
这是它的样子:
transfusion_date other
1 4/10/20 17:36 a
2 4/10/20 17:33 b
3 4/11/20 17:36 c
现在解析日期,然后按时间对数据集进行排序:
library(lubridate)
# parse date
o2outcomes$transfusion_date <- parse_date_time(o2outcomes$transfusion_date,
orders = "%m/%d/%y %H%M")
# sort
o2outcomes <- o2outcomes[order(o2outcomes$transfusion_date),]
o2outcomes
transfusion_date other
2 2020-04-10 17:33:00 b
1 2020-04-10 17:36:00 a
3 2020-04-11 17:36:00 c
推荐阅读
- javascript - 是否可以在浏览器上使用 javascript 文件重用另一个功能
- c++ - 为什么我不能将数组名称作为指针引用传递?
- python - 类 OfferSerializer 缺少“Meta.model”属性
- amazon-web-services - 在 AWS ubuntu 服务器中运行 pgadmin docker 容器
- webhooks - 在本地机器上设置 Google Drive webhook - 如何完全隐藏该站点,使其不被公众和机器人看到?
- ssh - 在 ssh 会话中从 systemd 显示颜色到嵌入式设备
- javascript - react-big-calendar 没有在其他组件中呈现任何内容
- javascript - 获取所选图像的完整路径
- git - git:将滞后分支合并到当前分支
- html - 在同一行中的两个 p 之间留出空间(flexbox)