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