2014-01-16 38 views
1

使用以下代码尝试将我的日期列转换为正确格式,但它只是将所有日期返回为“NA”。在R中使用as.Date转换为日期并获取一系列“NA”s

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

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

编辑:日期值如下:11/09/2009

+1

你能告诉我们的“日期”列的值。可能你在这里指定的格式不匹配 – RUser

+3

尝试'format =“%d /%m /%Y”'。 – Roland

回答

3

?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") 
+0

选择解决方案上的tic标记 - 使其成为anwser,如果这有助于您 – RUser

0

你可以尝试用包lubridate转换日期

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

后,你可以指定你想要的日期是哪一种格式:

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