如何将字符串因子转换为Date? [英] How to convert a string factor into Date?

查看:59
本文介绍了如何将字符串因子转换为Date?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用包含整数格式日期的伦敦犯罪数据集。我融化了它们,现在它们已成为因素 s。

I'm working with the London crime dataset which contains dates in an integer format. I melted them and now they have become factors.

例如, X200801 。我想将其转换为 2008年1月1日

For example, "X200801". I want to convert it into "1/1/2008".

我知道 as.Date 用于将不同类型的日期转换为日期格式,但我还是对数据集(即 X200801)中提供的这种日期格式有些困惑。 code>。

I know as.Date is used to convert different types into date format but still, I am a bit confused with this format of date which is provided in data set i.e "X200801".

这是我的结构

推荐答案

一种可能性是使用 lubridate

ss <- factor("X200801")

library(lubridate)
ymd(paste(sub("X", "", ss), "01", sep = ""))
#[1] "2008-01-01"

这篇关于如何将字符串因子转换为Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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