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

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

问题描述

我已经注意到几次处理日期不允许在 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")

推荐答案

怎么样

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天全站免登陆