0

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?

  • 2
    The function operates on a local copy of the data.frame. You should return the value then assign it back into the original if you want to alter the original df. `cambio <- function(d,v){ d[[v]][!is.na(d[[v]])] <- 0; return(d)}; df <- cambio(df,"A")` – dww Mar 21 '21 at 21:55
  • Great! It works. – Juan_Ramon Lacalle Mar 21 '21 at 22:31

2 Answers2

1

You have

d[[v]][!is.na(d[[v]])] <- 0

But this tells it to put a zero on any not NA, so you want:

cambio <- function(d,v){
  d[[v]][is.na(d[[v]])] <- 0
  return(d)
}

EDIT:: you're just missing the return(d) statement.

Justin Cocco
  • 392
  • 1
  • 6
0

Here's a few base R solutions:

one:

replace(df, df == 1, 0)

two:

replace(df, !is.na(df), 0)

three:

data.frame(lapply(df, pmin, 0))
hello_friend
  • 5,682
  • 1
  • 11
  • 15