时间日期格式[R]

时间:2017-06-26 11:31:24

标签: r date dataframe format time-series

我有一个time series数据框df

         date       lang     revenue   year month 
1     01/01/2014   DE-DE      8146.5   2014     1        
2     02/01/2014   EN-GB    31416.95   2014     1           
3     03/01/2014   EN-US      152646   2014     1              
4     04/01/2014   ES-ES       15523   2014     1        
5     05/01/2014   FR-FR      4221.5   2014     1        
6     06/01/2014   IT-IT     12971.7   2014     1           

...

我的目标是:创建一个取决于**week variable**日历的date并将其保存到我的df

  1. 我将date格式更改为factordate

    df$date = as.Date(df$date)

  2. 然而,结果变得像这样

    "0001-01-20" "0002-01-20" "0003-01-20" "0004-01-20" ...
    

    我尝试添加time format

    df$date = as.Date(df$date, format = "%d-%m-%y")

    但结果仍然相同。

    有什么问题?

    1. 创建一个取决于我的日期日历的周变量
    2. strftime(df$date,format="%W")

      然而,结果没有任何意义:

      "03" "03" "03" "03" "03" "03" "03" "03" "02" "02" "02" "02" "02" "02" "02" "02" "02" "03" "03" [20] "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" "03" [39] "03"

      我想知道哪里出错了? (当第一个问题解决后,第二个问题可能会解决?)

      谢谢!

1 个答案:

答案 0 :(得分:2)

我们需要指定format

 as.Date(df$date, "%d/%m/%Y")