Commit | Line | Data |
---|---|---|
3d69ff21 BA |
1 | fill_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 | { | |
12 | # print(line) | |
13 | for (i in 1:p) | |
14 | { | |
15 | # if (is.numeric(dat[line,i])) | |
16 | if (!is.na(dat[line,i])) | |
17 | { | |
18 | if (last_noNA_indices[i] < line-1) | |
19 | { | |
20 | #do some completion | |
21 | meanVal = ifelse(last_noNA_indices[i]>0, | |
22 | 0.5*(dat[last_noNA_indices[i],i]+dat[line,i]), | |
23 | dat[line,i]) | |
24 | ||
25 | for (j in (last_noNA_indices[i]+1):(line-1)) | |
26 | dat[j,i] = meanVal | |
27 | } | |
28 | last_noNA_indices[i] = line | |
29 | } | |
30 | } | |
31 | line = line + 1 | |
32 | } | |
33 | ||
34 | #complete until end if needed | |
35 | for (i in 1:p) | |
36 | { | |
37 | if (last_noNA_indices[i] < n) | |
38 | { | |
39 | for (j in (last_noNA_indices[i]+1):n) | |
40 | dat[j,i] = dat[last_noNA_indices[i],i] | |
41 | } | |
42 | } | |
43 | ||
44 | dat_with_date[,2:(p+1)] = dat | |
45 | write.csv(dat_with_date, "NONA.csv", row.names=FALSE) | |
46 | } |