1 fill_NAs = function(file)
3 dat_with_date = read.csv(file)
4 dat = dat_with_date[,-1] #get rid of date column
9 last_noNA_indices = rep(0, p)
14 if (!is.na(dat[line,i]))
16 if (last_noNA_indices[i] < line-1)
19 meanVal = ifelse( last_noNA_indices[i]>0,
20 0.5*(dat[last_noNA_indices[i],i]+dat[line,i]),
22 dat[(last_noNA_indices[i]+1):(line-1),i] = meanVal
24 last_noNA_indices[i] = line
30 #complete until end if needed
33 if (last_noNA_indices[i] < n)
34 dat[(last_noNA_indices[i]+1):n,i] = dat[last_noNA_indices[i],i]
37 dat_with_date[,2:(p+1)] = dat
38 write.csv(dat_with_date, "NONA.csv", row.names=FALSE)