Converting Monthly Data to Daily in R

前端 未结 5 1050
慢半拍i
慢半拍i 2021-01-18 08:02

I have a data.frame df that has monthly data:

Date           Value 
2008-01-01      3.5          
2008-02-01      9.5          
2008-03-01      0.1                   


        
5条回答
  •  抹茶落季
    2021-01-18 08:50

    Another way:

    library(lubridate)
    
    d <- read.table(text = "Date           Value 
                    2008-01-01      3.5          
                    2008-02-01      9.5          
                    2008-03-01      0.1",
                    stringsAsFactors = FALSE, header = TRUE)
    
    Dates <- seq(from = min(as.Date(d$Date)),
                 to = ceiling_date(max(as.Date(d$Date)), "month") - days(1),
                 by = "1 days")
    
    data.frame(Date = Dates,
               Value = setNames(d$Value, d$Date)[format(Dates, format = "%Y-%m-01")])
    

提交回复
热议问题