rparameter-passingpadr

Passing unquoted column list to fill_by_function in padr


I used padding (from padr package) on a data frame to fill the time gap. Now, to fill the gap values for a specified set of columns, I am using fill_by_function. In general, fill_by_function takes the unquoted column names as arguments. However, in my case, I have been provided with a list of column names.

My question is, how will I be able to pass the column list within fill_by_function function. Please note that the list of columns is not pre-defined, so I cannot hardcode the column names inside the fill_by_function.

Below is an example that I tried, but got an error.

x <- seq(as.Date('2016-01-01'), by = 'day', length.out = 366)
x <- x[sample(1:366, 200)] %>% sort
x.df <- data.frame(x  = x,
               y1 = runif(200, 10, 20) %>% round,
               y2 = runif(200, 1, 50) %>% round,
               y3 = runif(200, 20, 40) %>% round)

c.list <- c("y1","y2")
x.df %>% pad %>% fill_by_function(as.name(c.list),fun=mean)

Following is the error message that I got

Error in inds[i] <- which(colnames_x == as.character(cols[[i]])) : replacement has length zero

Is there any other alternative function that I can use


Solution

  • This worked for me:

    x.df %>% pad %>% fill_by_function(.cols=c.list,fun=mean) %>% tail(.)
    
                 x     y1    y2    y3
    361 2016-12-26 14.725 24.31 30.09
    362 2016-12-27 14.000 28.00 21.00
    363 2016-12-28 14.725 24.31 30.09
    364 2016-12-29 15.000 47.00 22.00
    365 2016-12-30 14.000 43.00 34.00
    366 2016-12-31 17.000 14.00 21.00
    

    Compare to:

    x.df %>% pad %>% fill_by_function(y1,fun=mean) %>% tail(.)
    
                 x     y1 y2 y3
    361 2016-12-26 14.725 NA NA
    362 2016-12-27 14.000 28 21
    363 2016-12-28 14.725 NA NA
    364 2016-12-29 15.000 47 22
    365 2016-12-30 14.000 43 34
    366 2016-12-31 17.000 14 21
    

    Check that the output is actually what you want.