I am passing some data.table
s to a function and want to collect the growing results in the passed data.table
s over multiple function calls. The rows are added (appended) within the function.
Is there a way to append rows to a data.table
"by reference/inplace"?
Any workaround if this is not possible?
Edit: My goal is adding multiple rows at once within the function and the number of rows can be very big (that's why I am using a 'data.table').
library(data.table)
validate <- function(data, rule, valid.result, checked.rules) {
# ... find errors
# How to append "rule" to "checked.rules"?
findings <- data.table(err.code = rule$rule.id, msg = "some blah blah") # just an stupid example
# How to append all "finding"s to "valid.results"?
}
data <- data.table(a=1:10, b=21:30)
valid.result <- data.table(err.code = integer(0), msg = character(0)) # empty validation results table
checked.rules <- data.table(rule.id = integer(0), rule.name = character(0)) # empty table
rules <- data.table(rule.id = 1:4, rule.name = c("too big", "too small", "too late", "empty"))
validate(data, rules[3, ], valid.result, checked.rules)
validate(data, rules[1, ], valid.result, checked.rules)
validate(data, rules[4, ], valid.result, checked.rules)
Expected results:
checked.rules
# rule.id rule.name
# 1: 3 too late
# 2: 1 too big
# 3: 4 empty
valid.results
# err.code msg
# 1: 3 some blah blah
# 2: 1 some blah blah
# 3: 4 some blah blah