strptime返回NA值 [英] strptime returning NA values

查看:89
本文介绍了strptime返回NA值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用strptime格式化我正在读取的日期,但输出中只会返回NA值.

I'm trying to use strptime to format dates I'm reading in but only get NA values are returned in the output.

我的原始数据格式为1974-01-01,数据集的长度为12049,所以最后一个日期为2006-12-31.

My raw data is in the format of 1974-01-01, and the length of the dataset is 12049 so the last date is 2006-12-31.

我使用的代码是:

Data$date.yyyymmdd <- as.POSIXct(strptime(Data$date.yyyymmdd, format = "%d/%m/%Y"))

对于这个问题的任何帮助或见解,将不胜感激!

Any help or insight into this problem would be very much appreciated!

推荐答案

您提供的format参数应该反映数据当前所处的格式,而不是您要将其转换为的格式,因此必须设置format = "%Y-%m-%d".再次阅读有关strptime的文档,这应该是有道理的.

The format argument you give should reflect the format that the data is currently in, not the format that you want to convert it to, so you would have to set format = "%Y-%m-%d". Read the documentation on strptime again and it should make sense.

这篇关于strptime返回NA值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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