data.frame - R - Transforming data in dataframe from one time scale to another -
data.frame - R - Transforming data in dataframe from one time scale to another -
i have 2 dataframes, each date column , several columns of numeric data. first dataframe, "daily", has info approximately not daily in periodicity while sec dataframe, "weekly", has info approximately not weekly in periodicity. accomplish replace "weekly" dataframe 1 has same dates "daily" , backfills info previous value missing values. know can utilize na.locf zoo bundle backfill info unsure how can introduce dates "daily" dataframe. perhaps variation of merge or join?
sample data:
daily<-data.frame( date1=rep(seq(as.date("2000-01-01"), as.date("2000-09-10"), by="1 day"), each=1), value1=runif(254), value2=rnorm(254), value3=rpois(254,10) ) weekly<-data.frame( date2=rep(seq(as.date("2000-01-01"), as.date("2000-09-10"), by="1 week"), each=1), value4=runif(37), value5=rnorm(37), value6=rpois(37,10) ) "result" should have date1 , value4, value5, , value6 columns , na.locf can used backfill missing data.
using zoo
, dplyr
:
library(dplyr) library(zoo) daily %>% left_join(weekly %>% select(date1 = date2,value4:value6)) %>% mutate_each(funs(na.locf),value4:value6) %>% head # don't maintain lastly function! ;) joining by: "date1" date1 value1 value2 value3 value4 value5 value6 1 2000-01-01 0.01670715 -0.6526126 9 0.3648553 0.775517 8 2 2000-01-02 0.21580455 -0.7702071 9 0.3648553 0.775517 8 3 2000-01-03 0.16307286 1.1770171 9 0.3648553 0.775517 8 4 2000-01-04 0.91464458 1.5960920 9 0.3648553 0.775517 8 5 2000-01-05 0.13975950 1.4407381 13 0.3648553 0.775517 8 6 2000-01-06 0.75104455 -0.5890481 11 0.3648553 0.775517 8
note utilize of select
rename date column same in both datsets.
r data.frame time-series
Comments
Post a Comment