关于在OSX 10.9.2的R 3.1.0中转换date

我有一列包含2种不同格式的date,即DD / MM / YY和D / M / YY。 由于Microsoft Excel(对于mac 2011,14.3.9)在部分variables中将D / M / YY标记为M / D / YY,因此输出date不正确。

然后我转向R,尝试将列转换为“DD-MON-YYYY”的格式,其中MON是月份的简写forms,如01-Jan-2014。 该列是这样的:

> head(date, 10) date 1 17/12/96 2 27/6/07 3 21/6/13 4 24/7/13 5 17/7/13 6 16/7/13 7 13/10/99 8 20/2/97 9 14/12/96 10 19/6/13 

我使用formatfunction

 format(date,"%d %b %Y") 

而输出是

 Error in format.default(structure(as.character(x), names = names(x), dim = dim(x), : invalid 'trim' argument 

我也尝试了lubridate包没有成功。

 > library(lubridate) > dmy(date) [1] NA Warning message: All formats failed to parse. No formats found. 

有没有简单的方法来改变date?

你需要把你的string转换成类date的对象,例如

 as.Date("17/12/96", "%d/%m/%y") [1] "1996-12-17" 

然后应用您的格式

 format(as.Date("17/12/96", "%d/%m/%y"), "%d-%b-%Y") [1] "17-Dec-1996"