1

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!

1 Answer 1

3

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))
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! So every time I write an anonymous function I need to return x just as I would a normal function? Does the ; in the anonymous function create a line break? If not, what purpose does it serve here?
a function, anonymous or not, returns the latest evaluation (when you don't use return). ; is indeed like a line break. Here, though you were modifying x, you were assigning 0, so that's what was returned. It's a common mistake (that i make occasionally :) ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.