as.Date返回NA,而从“ddmmmyyyy”转换 [英] as.Date returning NA while converting from 'ddmmmyyyy'

查看:611
本文介绍了as.Date返回NA,而从“ddmmmyyyy”转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串2013-JAN-14转换成如下日期:

I am trying to convert the string "2013-JAN-14" into a Date as follow :

sdate1 <- "2013-JAN-14"
ddate1 <- as.Date(sdate1,format="%Y-%b-%d")
ddate1

但我得到:

[1] NA

我做错了什么?我应该为此安装一个包(我试过安装chron)。

What am I doing wrong ? should I install a package for this purpose (I tried installing chron) .

推荐答案

适用于我。它不适用于您的原因可能与您的系统区域设置有关。

Works for me. The reasons it doesn't for you probably has to do with your system locale.

?as.Date 以下说:

## This will give NA(s) in some locales; setting the C locale
## as in the commented lines will overcome this on most systems.
## lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
z <- as.Date(x, "%d%b%Y")
## Sys.setlocale("LC_TIME", lct)

值得一试。

这篇关于as.Date返回NA,而从“ddmmmyyyy”转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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