Date format [R]

I have a data frame 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           

      

...

My goal: to create **week variable**

one that depends on the calendar date

and keep it in mine df

.

  • I changed the format date

    on factor

    todate

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

However, the results will be like

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

      

I tried to add time format

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

but the results are the same.

What is the problem?

  1. Create a weekly variable that depends on my date calendar

strftime(df$date,format="%W")

However, the results don't make any sense:

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

I'm wondering where is going wrong? (maybe the second question will be resolved when solving the 1st question?)

Thank!

+3


source to share


1 answer


We need to indicate format



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

      

+2


source







All Articles