我已经注意到几次使用日期不允许使用 R 中的常用技巧。假设我有一个带日期的数据帧 Data(见下文),我想将完整的数据帧转换为日期类。到目前为止,我能想到的唯一解决方案是:
for (i in 1:ncol(Data)){
Data[,i] <- as.Date(Data[,i],format="%d %B %Y")
}
这给出了具有正确结构的数据框:
> str(Data)
'data.frame': 6 obs. of 4 variables:
$ Rep1:Class 'Date' num [1:6] 12898 12898 13907 13907 13907 ...
$ Rep2:Class 'Date' num [1:6] 13278 13278 14217 14217 14217 ...
$ Rep3:Class 'Date' num [1:6] 13600 13600 14340 14340 14340 ...
$ Rep4:Class 'Date' num [1:6] 13831 13831 14669 14669 14669 ...
使用经典的应用方法会带来完全不同的东西。尽管所有变量都属于同一类并且属于同一类,但我无法获得正确类的数据框或矩阵作为输出:
> str(sapply(Data,as.Date,format="%d %B %Y"))
num [1:6, 1:4] 12898 12898 13907 13907 13907 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:4] "Rep1" "Rep2" "Rep3" "Rep4"
> str(apply(Data,2,as.Date,format="%d %B %Y"))
num [1:6, 1:4] 12898 12898 13907 13907 13907 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:4] "Rep1" "Rep2" "Rep3" "Rep4"
如果您想在 Date 对象中再次转换这些矩阵,您需要一个原点。该起源可能因系统而异,因此在 apply() 之后使用 as.Date 或其他函数也无济于事。如果你应用原点,你会再次得到一个向量。
有人对这种数据有一个干净的解决方案吗?下面是我在示例中使用的数据框。
Data <- structure(list(Rep1 = c(" 25 April 2005 ", " 25 April 2005 ",
" 29 January 2008 ", " 29 January 2008 ", " 29 January 2008 ",
" 29 January 2008 "), Rep2 = c(" 10 May 2006 ", " 10 May 2006 ",
" 4 December 2008 ", " 4 December 2008 ", " 4 December 2008 ",
" 4 December 2008 "), Rep3 = c(" 28 March 2007 ", " 28 March 2007 ",
" 6 April 2009 ", " 6 April 2009 ", " 6 April 2009 ", " 6 April 2009 "
), Rep4 = c(" 14 November 2007 ", " 14 November 2007 ", " 1 March 2010 ",
" 1 March 2010 ", " 1 March 2010 ", " 1 March 2010 ")), .Names = c("Rep1",
"Rep2", "Rep3", "Rep4"), row.names = c("1", "2", "3", "4", "5",
"6"), class = "data.frame")