Michael
Michael

Reputation: 85

Anonymous function in R not returning expected output

I have this dataframe:

df <- data.frame(c(NA,2,3,1),c(4,NA,6,1),c(7,8,NA,9),c(1,5,6,4))
colnames(df) <- c('x1','x2','x3','x4')`

If I write a simple imputation function that imputes NA values as such:

impute <- function(x, imputation.method) { 
x[is.na(a)] <- imputation.method
x
}

And call it on the first two columns in the original dataframe. It imputes the NA values properly as such:

library(purrr)
df[,c('x1', 'x2')] <- map(df[,c('x1', 'x2')], impute, 0)
df

# x1 x2 x3 x4    
# 0  4  7  1     
# 2  0  8  5     
# 3  6 NA  6    
# 1  1  9  4  

Then if I try to do it in a one-liner using an anonymous function it returns the following undesired output:

df[,c('x1', 'x2')] <- map(df[,c('x1', 'x2')], function(x){x[is.na(x)] <- 0 })

# x1 x2 x3 x4    
# 0  0  7  1     
# 0  0  8  5     
# 0  0 NA  6    
# 0  0  9  4    

I am unaware as to why rewriting my original function the same as a one-line anonymous function changes the output. Any input as to why this is occurring and how change the anonymous function to return the desired output is appreciated!

Upvotes: 1

Views: 31

Answers (1)

moodymudskipper
moodymudskipper

Reputation: 47300

There you go!

  df[,c('x1', 'x2')] <- map(df[,c('x1', 'x2')], function(x){x[is.na(x)] <- 0;x })
  #   x1 x2 x3 x4
  # 1  0  4  7  1
  # 2  2  0  8  5
  # 3  3  6 NA  6
  # 4  1  1  9  4

your function was returning 0, it needs to return the modified x

Here's another way to write it:

mutate_at(df, c('x1', 'x2'), ~`[<-`(., is.na(.), value = 0))

Upvotes: 3

Related Questions