季度数据-将因子转换为日期

时间:2019-02-04 23:05:54

标签: r date lubridate posixct

无法将日期列从因子转换为日期类型。我已经尝试过基础r和润滑策略:

> as.Date(df)
Error in as.Date.default(x, ...) : 
  do not know how to convert 'x' to class “Date”

> yq(df)
[1] NA
Warning message:
All formats failed to parse. No formats found. 

数据:

df<- structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2008-07-31", 
"2009-07-31", "2010-07-31", "2011-07-31", "2012-07-31", "2013-07-31", 
"2014-07-31", "2011-10-31", "2012-10-31", "2013-10-31", "2014-10-31", 
"2016-10-31", "2017-10-31"), class = "factor")), row.names = c(NA, 
6L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

as.Date应用于Date列,而不是整个data.frame。不使用任何软件包。

transform(df, Date = as.Date(Date))

给予:

        Date
1 2008-07-31
2 2008-07-31
3 2008-07-31
4 2008-07-31
5 2008-07-31
6 2008-07-31

答案 1 :(得分:0)

以上答案显示了您的问题的基本R方法。我已经使用tidyverse包来做相同的事情,就像做事的不同方式一样。

首先,我们将查看您的数据

df<- structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2008-07-31", 
"2009-07-31", "2010-07-31", "2011-07-31", "2012-07-31", "2013-07-31", 
"2014-07-31", "2011-10-31", "2012-10-31", "2013-10-31", "2014-10-31", 
"2016-10-31", "2017-10-31"), class = "factor")), row.names = c(NA, 
6L), class = "data.frame")

print(df)

        Date
1 2008-07-31
2 2008-07-31
3 2008-07-31
4 2008-07-31
5 2008-07-31
6 2008-07-31

如果我们看一下数据框的结构,我们可以看到日期存储为因子

str(df)

'data.frame':   6 obs. of  1 variable:
 $ Date: Factor w/ 13 levels "2008-07-31","2009-07-31",..: 1 1 1 1 1 1

我们需要加载一些库。唯一需要说明的是magrittr包,它提供了方便的%<>%运算符,该运算符将管道右侧的运算结果分配给原始变量

library(tidyverse)
library(lubridate)
library(magrittr)

因此,在下面的管道中,我们-

  • 通过df将日期的as_tibble()数据帧作为因子传送,将其转换为tidyverse tibble格式
  • 然后使用mutate函数覆盖Date
  • 将其值替换为lubridate函数ymd的结果,该函数是一种便捷函数,用于以year-month-date结构解析因子或字符串格式的日期。如果数据为dmy格式,则等效项可能为day-month-year
df %<>%
  as_tibble() %>%
  mutate(
    Date = ymd(Date)
  )

现在完成了,我们可以查看数据

print(df)
# A tibble: 6 x 1
  Date      
  <date>    
1 2008-07-31
2 2008-07-31
3 2008-07-31
4 2008-07-31
5 2008-07-31
6 2008-07-31

现在您可以看到日期现在存储为date数据类型。

最终评论-

如果您想手动创建数据表或tibbles,请看一下tribble函数,如下所示,我们在其中手动创建与您相同的小标题,然后转换数据类型到factor

df <- tribble(
    ~Date,
    "2008-07-31", 
    "2009-07-31", 
    "2010-07-31", 
    "2011-07-31", 
    "2012-07-31", 
    "2013-07-31", 
    "2014-07-31", 
    "2011-10-31", 
    "2012-10-31", 
    "2013-10-31", 
    "2014-10-31", 
    "2016-10-31", 
    "2017-10-31") 


df %<>%
  mutate(
    Date = as_factor(Date)
  )

glimpse(df)

Observations: 13
Variables: 1
$ Date <fct> 2008-07-31, 2009-07-31, 2010-07-31, 2011-07-31, 2012-07-31, 2013-07-31, 2014-07-31, 2011-10-31, 2012-10-31, 2013-10-31, 2014-10-31...