R,不一致的日期格式 [英] R, inconsistent date format

查看:21
本文介绍了R,不一致的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期变量,它最初来自一个 excel.然而,它是如此的异类.尽管在 excel 中看起来都像 yyyy/mm/dd,但在 R 中读取时,变量看起来像:

I have a date variable, which originally comes from an excel. However, it is so heterogeneous. Even though all look like yyyy/mm/dd in the excel, when read in R, the variable look like:

person_1  39257
person_2  2015/2/20
person_3  NA

如何清理日期变量,让每个都显示yyyy/mm/dd格式?

How to clean up the date variable so that every and each shows yyyy/mm/dd format?

推荐答案

或者带有anydateexcel_numeric_to_date的选项

library(janitor)
library(anytime)
library(dplyr)
coalesce( excel_numeric_to_date(as.numeric(dat$V2)), anydate(dat$V2))
#[1] "2007-06-24" "2015-02-20" NA   

数据

dat <- structure(list(V1 = c("person_1", "person_2", "person_3"), V2 = c("39257", 
"2015/2/20", NA)), class = "data.frame", row.names = c(NA, -3L
))

这篇关于R,不一致的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆