I have written a function to change values not being NA in each column into a new value. The following example illustrates the problem:
df <- data.frame(A=c(1,NA,1,1,NA),
B=c(NA,1,NA,1,NA),
C=c(1,NA,1,NA,1))
1's should be changed into 0's with the function:
cambio <- function(d,v){
d[[v]][!is.na(d[[v]])] <- 0
}
The column is named within the function with [[]], and it is passed with quotes as argument to the function. I learned this in a clear and useful response to the post Pass a data.frame column name to a function. However, after running the function, for example, with the first variable,
cambio(df,"A")
the values of tha column keep unchanged. Why this function does not work as expected?