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

Asked

Viewed 1,347 times

2

I need to check if there are columns (i.e., variables) inside a data.frame before doing any operation, but the past variable is not in string to compare with the colnames or names.

Follows the code:

check_measurement_ages = function(data_base,measurement_variable1,measurement_variable2){
  if(measurement_variable1 %in% names(data_base)){
}
  • I don’t understand. Your question is ambiguous to me. Do you need to check if there are any columns (either way) in the data frame or do you need to look for a specific column? If it’s a specific column, how do we identify it? What it has of special to differentiate it from the other columns that perhaps exist in this data frame?

1 answer

1

Your problem is related to non-standard evaluation.

The smallest 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 function substitute captures user-typed expression, and function deparse turns it into a string.

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

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

> verificar_coluna2(mtcars, mpg)
[1] FALSE

So a safer approach is proposed by the package lazyeval. To Vignette of it is very explanatory.

In practice, it is better to write a function:

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

Thus the two functions will function.

> verificar_coluna(mtcars, mpg)
[1] TRUE
> verificar_coluna2(mtcars, mpg)
[1] TRUE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.