I have a data frame with a variable number of columns (.csv import). Column 1 is always the same (timecodes), column 2 always exists and the other columns may exist or not (so there's either 2 or i columns). Columns 2 - n show meter readings of maschine energy consumption. Sometimes values are missing. I want to interpolate those missing values.
df:
time maschine1 maschine 2 maschine 3 ...
16:15 7960 8237 9475 ...
16:20 10480 10757 11995 ...
16:25 NA NA NA ...
16:30 15520 15797 17035 ...
16:35 18160 18437 19675 ...
Here's what I'm trying:
for(i in 2:ncol(df)) {
df <- df %>%
mutate(maschine_[i]_interpolated = (ifelse(is.na(.[[i]]),(lag(df[[i]])+lead(df[[i]))/2, .[[i]])))
}
So I want to interpolate and write the interpolated values in a new column for each of the i to n maschines.
Any ideas? Thanks a lot in advance!