如何将字符的数据框转换为相应的日期? [英] How to transform a dataframe of characters to the respective dates?

查看:136
本文介绍了如何将字符的数据框转换为相应的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到已经有几次使用日期不允许在R中使用通常的技巧。说我有一个数据帧数据与日期(见下文),我想将完整的数据框转换为日期类。到目前为止,我可以想出的唯一解决方案是:

I noticed already a couple of times that working with dates doesn't allow for using the usual tricks in R. Say I have a dataframe Data with Dates (see below), and I want to convert the complete dataframe to a date class. The only solution I could come up with until now is :

for (i in 1:ncol(Data)){
    Data[,i] <- as.Date(Data[,i],format="%d %B %Y")
}

这给出了一个具有正确结构的数据框:

This gives a dataframe with the correct structure :

> 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 ...

使用经典的应用方法给出了完全不同的东西。虽然所有变量都是同一个类,并且转到同一个类,但是我无法获取正确类的数据框或矩阵作为输出:

Using a classic apply approach gives something completely different. Although all variables are of the same class and go to the same class, I can't get a data-frame or matrix of the correct class as output :

> 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或另一个函数也没有帮助。如果你应用原点,你再次得到一个向量。

If you want to transform these matrices again in Date objects, you need an origin. That origin can differ from system to system, so using as.Date or another function after the apply() doesn't help much either. If you apply the origin, you get a vector again.

任何人都是这种数据的干净的解决方案?以下是示例中使用的数据框。

Anybody a clean solution for this kind of data? Below is the dataframe I used in the examples.

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")


推荐答案

b
$ b

How about

str(as.data.frame(lapply(Data,as.Date,format="%d %B %Y")))
# '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 ...

这篇关于如何将字符的数据框转换为相应的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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