as.Date 在从 'ddmmmyyyy' 转换时返回 NA [英] as.Date returning NA while converting from 'ddmmmyyyy'

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

问题描述

我正在尝试将字符串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 在从 'ddmmmyyyy' 转换时返回 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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