5
5
# ' @param new The new data.frame
6
6
# ' @return A matrix in which each row represents a change from the old to the
7
7
# ' new matrix in the form of [row, col, newVal, oldVal].
8
- # ' @author Jeff Allen \email{jeff@@trestletech.com}
8
+ # ' @author Jeff Allen \email{jeff@@trestletech.com}, Jonathan Owen \email{jonathanro@@gmail.com}, Tadeas Palusga \email{tadeas@@palusga.cz}
9
9
# ' @export
10
10
calcHtableDelta <- function (old , new , zeroIndex = TRUE ){
11
- changes <- matrix (ncol = 4 , nrow = 0 )
12
- colnames(changes ) <- c(" row" , " col" , " new" , " old" )
11
+ changes <- NULL
13
12
14
13
# Loop through each column, comparing the data
15
14
for (i in 1 : (max(ncol(new ), ncol(old )))){
@@ -18,36 +17,41 @@ calcHtableDelta <- function (old, new, zeroIndex = TRUE){
18
17
19
18
if (i > ncol(new )){
20
19
# the new data.frame doesn't have this column
21
- thisColChanges <- matrix (c(1 : nrow(old ),
22
- rep(i , nrow(old )),
23
- rep(NA , nrow(old )),
24
- old [,i ])
25
- , ncol = 4 )
20
+ thisColChanges <- data.frame (1 : nrow(old ),
21
+ rep(i , nrow(old )),
22
+ rep(NA , nrow(old )),
23
+ old [,i ])
26
24
} else if (i > ncol(old )){
27
25
# The old data.frame doesn't have this column
28
- thisColChanges <- matrix (c(1 : nrow(new ),
29
- rep(i , nrow(new )),
30
- new [,i ],
31
- rep(NA , nrow(new )))
32
- , ncol = 4 )
26
+ thisColChanges <- data.frame (1 : nrow(new ),
27
+ rep(i , nrow(new )),
28
+ new [,i ],
29
+ rep(NA , nrow(new )))
33
30
} else {
34
31
# They both have this column
35
32
deltaInd <- which(suppressWarnings(old [,i ] != new [,i ]))
36
33
lng <- length(deltaInd )
37
34
38
- thisColChanges <- matrix (c(deltaInd ,
39
- rep(i , lng ),
40
- new [deltaInd , i ],
41
- old [deltaInd , i ])
42
- , ncol = 4 )
35
+ thisColChanges <- data.frame (deltaInd ,
36
+ rep(i , lng ),
37
+ new [deltaInd , i ],
38
+ old [deltaInd , i ])
43
39
}
44
40
41
+ if (is.logical(thisColChanges [, 3 ]))
42
+ thisColChanges [, 3 ] = ifelse(thisColChanges [, 3 ], " true" , " false" )
43
+ if (is.logical(thisColChanges [, 4 ]))
44
+ thisColChanges [, 4 ] = ifelse(thisColChanges [, 4 ], " true" , " false" )
45
+
45
46
if (zeroIndex && nrow(thisColChanges ) > 0 ){
46
47
thisColChanges [,1 ] <- as.integer(thisColChanges [,1 ]) - 1 ;
47
48
thisColChanges [,2 ] <- as.integer(thisColChanges [,2 ]) - 1 ;
48
49
}
49
-
50
50
changes <- rbind(changes , thisColChanges )
51
51
}
52
+
53
+ if (! is.null(changes )) {
54
+ colnames(changes ) <- c(" row" , " col" , " new" , " old" )
55
+ }
52
56
return (changes )
53
57
}
0 commit comments