I have two data.frames df1 and errors and a function add_to_errors(). The argument of the function is a vector of row-numbers (of df1) and the function should add add the row selected to a new data.frame called errors.
df1 <- data.frame(id=1:10, var1=rep(NA, 2), var2=rep(NA, 2)) errors <- data.frame() add_to_errors <- function(ids){ rows_to_add <- df1[ids,] errors <- rbind(errors, rows_to_add) return(errors) } add_to_errors(c(1,2)) add_to_errors(c(6,7)) When I execute add_to_errors(1, 2) and add_to_errors(6,7), it looks as if errors was overwritten each time the function is called. How can I access errors as a global variable within the function?
The output should look like this:
id var1 var2 1 1 NA NA 2 2 NA NA 3 6 NA NA 4 7 NA NA