How to check if a (variable) column exists inside a 'data.frame'?

2

I need to check for columns (or variables) within data.frame before doing any operation, but the passed variable is not in string to compare with colnames or names . >

Follow the code:

check_measurement_ages = function(data_base,measurement_variable1,measurement_variable2){
  if(measurement_variable1 %in% names(data_base)){
}
    
asked by anonymous 27.09.2016 / 08:15

1 answer

1

Your issue is related to non-standard evaluation .

The least possible example of doing this is:

verificar_coluna <- function(data, coluna){
  coluna_texto <- deparse(substitute(coluna))
  coluna_texto %in% names(data)
}

> verificar_coluna(mtcars, mpg)
[1] TRUE

The substitute function captures the expression entered by the user, and the deparse function turns it into a string.

The problem with this approach is if your function is called from within another:

verificar_coluna2 <- function(data, coluna){
  verificar_coluna(data, coluna)
}

> verificar_coluna2(mtcars, mpg)
[1] FALSE

So a safer approach is proposed by the lazyeval package. The vignette is very explanatory.

In practice, it's best to write a function:

verificar_coluna <- function(data, coluna){
  coluna_texto <- lazyeval::expr_text(coluna)
  coluna_texto %in% names(data)
}

So the two functions will work.

> verificar_coluna(mtcars, mpg)
[1] TRUE
> verificar_coluna2(mtcars, mpg)
[1] TRUE
    
27.09.2016 / 13:45