0

I have a R data frame, which daily level data with a single date column and rest 4-5 other columns, i am using the pad function in dplyer to fill in missing dates within a series of data. Group2 is a list of column name stored in a variable.

data_padded <- data_df%>% pad(group = GROUP2)

but then it always pads for a day interval rather than determining from the data if it needs day, week, month etc intervals as the data has column name "Date_level" which has values like 'daily', weekly', 'monthly'.

If we can somehow use the date_level column in the dataframe to say,

if Date_level=' weekly' then 
padded_data <- data_padded %>%
 pad(group = GROUP2, interval = "week")
if Date_level='daily' then 
data_padded <- data_padded %>% pad(group = GROUP2, interval = "day")
if Date_level='monthly' then 
padded_data <- data_padded %>% pad(group = GROUP2, interval = "month")

so that I could the interval property in pad function to pad according the date level.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ritzen101
  • 27
  • 2
  • 6
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 06 '23 at 19:00
  • Try with `group_modify`: `data_df %>% group_by(Date_level) %>% group_modify(~ .y %>% pad( group = GROUP2, interval = if(.x == 'weekly') "week" else if (.x=='daily') "day" else if(.x=='montly') "month" else NULL))` . As you didnt share an example, i don't known if this works in your case. ` – Ric Feb 06 '23 at 19:12

0 Answers0