better style in report
[talweg.git] / data / scripts / fill_NAs.R
CommitLineData
3d69ff21
BA
1fill_NAs = function(file)
2{
3 dat_with_date = read.csv(file)
4 dat = dat_with_date[,-1] #get rid of date column
5
6 line = 1
7 n = nrow(dat)
8 p = ncol(dat)
9 last_noNA_indices = rep(0, p)
10 while (line <= n)
11 {
3d69ff21
BA
12 for (i in 1:p)
13 {
3d69ff21
BA
14 if (!is.na(dat[line,i]))
15 {
16 if (last_noNA_indices[i] < line-1)
17 {
18 #do some completion
da7f46b8
BA
19 meanVal = ifelse( last_noNA_indices[i]>0,
20 0.5*(dat[last_noNA_indices[i],i]+dat[line,i]),
21 dat[line,i] )
22 dat[(last_noNA_indices[i]+1):(line-1),i] = meanVal
3d69ff21
BA
23 }
24 last_noNA_indices[i] = line
25 }
26 }
27 line = line + 1
28 }
29
30 #complete until end if needed
31 for (i in 1:p)
32 {
33 if (last_noNA_indices[i] < n)
da7f46b8 34 dat[(last_noNA_indices[i]+1):n,i] = dat[last_noNA_indices[i],i]
3d69ff21
BA
35 }
36
37 dat_with_date[,2:(p+1)] = dat
38 write.csv(dat_with_date, "NONA.csv", row.names=FALSE)
39}