简体   繁体   English

在R中使用as.Date转换为日期并获得“NA”系列

[英]Using as.Date in R to convert to dates and getting series of “NA”s

Using the following code to try to get my date column into the right format, but it's just returning all of the dates as "NA". 使用以下代码尝试将我的日期列设置为正确的格式,但它只是将所有日期返回为“NA”。

setwd("C:\\Users\\user\\Documents\\Files\\a")
data <- read.csv("file1.csv")

data$Date <- as.Date(data$Date, format = "d%/m%/Y%")

Edit: date values as follows: 11/09/2009 编辑:日期值如下:2009年9月11日

来自?as.Date文档:

If the date string does not specify the date completely, the returned answer may be system-specific. The most common behaviour is to assume that a missing year, month or day is the current one. If it specifies a date incorrectly, reliable implementations will give an error and the date is reported as 'NA'. Unfortunately some common implementations (such as 'glibc') are unreliable and guess at the intended meaning.

data$Date <- as.Date(data$Date, format = "%d/%m/%Y")

You could try converting the date with the package lubridate 您可以尝试使用包lubridate转换日期

library(lubridate)
date <- parse_date_time(x = date,
                orders = c("d m y", "d B Y", "mdy"),
                locale = "eng")

After you may specify which format you want the date to be: 您可以指定日期的格式:

character_date <- as.character(as.Date(date, "%m/%d/%Y"), format = "%m/%d/%Y")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM