R: Pass data.frame by reference to a function
问题 I pass a data.frame as parameter to a function that want to alter the data inside: x <- data.frame(value=c(1,2,3,4)) f <- function(d){ for(i in 1:nrow(d)) { if(d$value[i] %% 2 == 0){ d$value[i] <-0 } } print(d) } When I execute f(x) I can see how the data.frame inside gets modified: > f(x) value 1 1 2 0 3 3 4 0 However, the original data.frame I passed is unmodified: > x value 1 1 2 2 3 3 4 4 Usually I have overcame this by returning the modified one: f <- function(d){ for(i in 1:nrow(d)) {