如何将多种日期格式转换为`as.posixct`?

杜沙尔

嗨,我的数据中有多种日期格式。如何转换为日期格式as.posixct
以下是示例数据。

df <- data.frame(date=c('01-12-2019','44166','12-08-2019','01/27/2020'))

任何帮助将不胜感激。

尼尔森·冈

一种可能是分别处理不同的日期格式(使用中的tryFormats参数可能会发生这种情况as.Date但是日期格式缺乏统一性,因此使用起来不太可能。):

df$date <-stringr::str_replace_all(df$date,"/","-") #format won't work without this
good_formats <-df[grepl("\\D",df$date),]
bad_formats<- df[!grepl("\\D",df$date),] #They're not bad, just think they're less programmer friendly
good_formats<-as.Date(good_formats,
       format=c("%d-%m-%Y","%d-%m-%Y","%m-%d-%Y"))
bad_formats <- as.Date(as.numeric(bad_formats),origin="1970-01-01")
data.frame(date=c(good_formats,bad_formats))

结果:

   date
1 2019-12-01
2 2019-08-12
3 2020-01-27
4 2090-12-03

结果的结构:

'data.frame':   4 obs. of  1 variable:
 $ date: Date, format: "2019-12-01" ...

数据:

data.frame(date=c('01-12-2019','44166','12-08-2019','01/27/2020')

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将数字时间转换为R中的日期时间POSIXct格式

来自分类Dev

将日期转换为POSIXct

来自分类Dev

POSIXct日期转换错误

来自分类Dev

R POSIXct和日期

来自分类Dev

如何创建POSIXct矩阵

来自分类Dev

将POSIXct转换回因子

来自分类Dev

r-将POSIXct转换为毫秒

来自分类Dev

R-如何使用as.POSIXct将JavaScript时间戳转换为人类日期时间?

来自分类Dev

将变量从“ dttm”转换为“ POSIXCT”

来自分类Dev

使用sapply将列转换为posixct并在R中保留日期时间格式

来自分类Dev

使用as.POSIXct将日期从“字符”格式转换为“ POSIXct”时,1个特定日期不适用

来自分类Dev

将列转换为POSIXct日期/时间序列

来自分类Dev

rbind将POSIXct转换为字符-如何转换回POSIXct?

来自分类Dev

将POSIXct日期时间转换为Date时出现意外日期-时区可以解决吗?

来自分类Dev

转换为POSIXct时出错:某些日期返回NA

来自分类Dev

为POSIXct日期格式添加因子列

来自分类Dev

如何将数据框中的列转换为POSIXct类?只有月份和年份

来自分类Dev

将数字时间转换为R中的日期时间POSIXct格式

来自分类Dev

将GMT格式的chron转换为POSIXct

来自分类Dev

R:将“日期/时间”列转换为POSIXct

来自分类Dev

使用 as.POSIXct 用 R 转换日期

来自分类Dev

POSIXct 和时区转换

来自分类Dev

无法转换为 POSIXct (R)

来自分类Dev

将 14 位日期时间对象转换为 POSIXct

来自分类Dev

ggplot 中的 xlim 与 POSIXct 日期

来自分类Dev

将因子数据转换为 POSIXct 格式

来自分类Dev

如何将包含日期 (YYYYMMDD) 的数字变量转换为 POSIXct 格式变量

来自分类Dev

如何从 POSIXct 范围中对 POSIXct 进行子集

来自分类Dev

无法将不同格式的向量转换为 POSIXct

Related 相关文章

热门标签

归档