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

查看:173
本文介绍了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天全站免登陆