r - Adding rows after specific timestamp -


short story: - while doing analysis, forgot account inter-day , inter-week values. need add them table.

long story: have table:

library(tidyverse) library(lubridate) df<-structure(list(time = structure(c(1488987000, 1488988800, 1488990600,                                        1488992400, 1488994200, 1488996000, 1488997800, 1488999600, 1489001400,                                        1489003200, 1489005000, 1489006800, 1489069800, 1489071600, 1489073400,                                        1489075200, 1489077000, 1489078800, 1489080600, 1489082400, 1489084200,                                        1489086000, 1489087800, 1489089600, 1489091400, 1489093200, 1489156200,                                        1489158000, 1489159800, 1489161600, 1489163400, 1489165200, 1489167000,                                        1489168800, 1489170600, 1489172400, 1489174200, 1489176000, 1489177800,                                        1489179600, 1489411800, 1489413600, 1489415400, 1489417200, 1489419000 ), class = c("posixct", "posixt"), tzone = structure("america/new_york", .names = "tz")),  low.yields = c(0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1,                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)), .names = c("time",                                                                  "low.yields"), row.names = c(na, -45l), class = c("tbl_df", "tbl",                                                                                                                    "data.frame")) 

there 2 columns, timestamps, , dummy variable.

i add , replicate rows have visible hour in timestamp 16:00 , change timestamp 9:00, in same day next row. notice not have next day, on weekend well.

up till found function tibble::add_row, not sure how replicate specific rows. however, if do

df %>% add_row(.after = which(hour(df$time) == 16))  

i @ least empty row first value, not all.

at moment, table looks this:

   time                 low.yields    ...                  ... 26 2017-03-09 16:00:00  1 27 2017-03-10 09:30:00  0    ...                  ... 

afterwards, make this. time half hour smaller row below, , low.yields value inherited row above. done every row there time 16:00.

   time                 low.yields    ...                  ... 26 2017-03-09 16:00:00  1 27 2017-03-10 09:00:00  1 28 2017-03-10 09:30:00  0    ...                  ... 

so after big @akrun, came solution problem.

df %>%     filter(hour(time)==16) %>%     mutate(time = as.posixct(paste(as.date(time), "09:00:00"), tz = "america/new_york") ) %>%      mutate(time = time + days(ifelse(wday(time) == 6, 3, 1))) %>%     bind_rows(df, .) %>%      arrange(time)  

thanks lot help.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -