Question:

My machine only sends an update to the cloud if the value of a variables has changed. As a result there are empty entries in my analytics query. 

How can i fill up these empty cells in my data (upwards/downwards/only at the end/start)?


Solution:

To easily solve this problem we can use the "fill" function which is provided by the "tidyr" package in R.


# Loading tidyr package
library(tidyr)

# Applying fill operation
df <- fill(df, columns, .direction = "up")


There are only 2 parameters required for this function:

    df .... data frame you want to manipulate

    columns ... definition of columns which should be filled

    .direction ... filling direction (default is "down")


For more information see https://tidyr.tidyverse.org/reference/fill.html 



To fill up only the start (with the first non NA value) or the end (with the last non NA value) use the following code snippet:


minmax <- range(which(!is.na(df$column)))

# fills the start
df$column[1:minmax[1]] <- df$column[minmax[1]]

#fills the end
df$column[minmax[2]:length(df$column)] <- df$column[minmax[2]] 

    

    The required parameters are: 

df .... data frame you want to manipulate

columns ... definition of columns which should be filled