Hazard
Hazard

Reputation: 3

Passing column name to function in R

I am removing NAs from a number of columns of my dataframe. My dataframe name is pretty long but that is required. It looks a bit messy using the command over and over again so I want to make a function that makes it easier to understand what's happening. I did the following:-

ConvNAtoBlank <- function(colname) {
    dataframe[[colname]][is.na(dataframe[[colname]])] = ""
}

This did not work. I have also tried to return the dataframe and assign it again later as:-

dataframe <- ConvNAtoBlank("B") # assuming B is a column name

This does not seem to work either. Is there anything I'm doing wrong here? I started learning R this Monday so I'm still a newbie and I can't figure this out. Any help would be appreciated.

Upvotes: 0

Views: 39

Answers (2)

akrun
akrun

Reputation: 886938

We could use tidyverse methods to pass either quoted/unquoted arguments

library(dplyr)
library(tidyr)
ConvNAtoBlank <- function(dataframe, colname) {
     colname <- rlang::ensym(colname)
     dataframe %>%
       mutate(!! colname := replace_na(!! colname, ""))
   }

-testing

df <- data.frame(A = c('A', NA, 'B'), B = c(NA, NA, 'A'))
ConvNAtoBlank(df, "B")
  A B
1    A  
2 <NA>  
3    B A
ConvNAtoBlank(df,  B)
     A B
1    A  
2 <NA>  
3    B A

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388807

You need to return the changed dataframe back at the end of the function. It is a good practice to pass dataframe to the function as well.

ConvNAtoBlank <- function(dataframe, colname) {
  dataframe[[colname]][is.na(dataframe[[colname]])] = ""
  dataframe
}

df <- data.frame(A = c('A', NA, 'B'), B = c(NA, NA, 'A'))
ConvNAtoBlank(df, "B")

#     A B
#1    A  
#2 <NA>  
#3    B A

Upvotes: 1

Related Questions