as.POSIXct does not recognise date format = “%Y-%W”

后端 未结 1 1451
春和景丽
春和景丽 2021-01-25 19:00
library(xts)

data <- data.frame(year_week = c(\"2016-46\", \"2016-47\", \"2016-48\"),
                   satisfaction = c(0.25, 0.45, 0.58))

data = xts(data[-1], or         


        
1条回答
  •  野的像风
    2021-01-25 19:23

    This is a variant on a quasi-FAQ on 'by can one not parse year and month as a date': because a date is day and month and year.

    Or year, a year and week and day. Otherwise you are indeterminate:

    > as.Date(format(Sys.Date(), "%Y-%W-%d"), "%Y-%W-%d")
    [1] "2017-12-04"
    > 
    

    using

    > Sys.Date()
    [1] "2017-12-04"
    > format(Sys.Date(), "%Y-%W-%d")
    [1] "2017-49-04"
    > 
    

    so %W works on input and output provided you also supply a day.

    For input data that does not have a day, you could just add a given weekday, say 1:

    > as.Date(paste0(c("2016-46", "2016-47", "2016-48"), "-1"), "%Y-%W-%w")
    [1] "2016-11-14" "2016-11-21" "2016-11-28"
    > 
    

    0 讨论(0)
提交回复
热议问题