To find whether a column exists in data frame or not

I have a data.frame with the name "abcframe"

     a  b  c
     1  1  1
     2  2  3

How might I find whether a column exists or not in a given data frame? For example, I would like to find whether a column d exists in the data.frame abcframe.


Assuming that the name of your data frame is dat and that your column name to check is "d", you can use the %in% operator:

if("d" %in% colnames(dat))
{
  cat("Yep, it's in there!\n");
}

You have a number of options, including using %in% and grepl:

dat <- data.frame(a=1:2, b=2:3, c=4:5)
dat
  a b c
1 1 2 4
2 2 3 5

To get the names of the columns:

names(dat)
[1] "a" "b" "c"

Use %in% to check for membership:

"d" %in% names(dat)
[1] FALSE

Or use `grepl` to check for a match:

grepl("d", names(dat))
[1] FALSE FALSE FALSE

You could use any:

> names(dat)
[1] "a" "b" "c"
> any(names(dat) == 'b')
[1] TRUE
> any(names(dat) == 'B')
[1] FALSE

You could also use if(!is.null(abcframe$d)) to test whether d exists in abcframe.

dat <- data.frame(a = 1:2, b = 2:3, c = 4:5)

if (!is.null(dat$d)) {
  print("d exists")
} else {
  print("d does not exist")
}
if (!is.null(dat$a)) {
  print("a exists")
} else {
  print("a does not exist")
}

A tidyverse approach might be more readable for some people, and therefore better to remember.

You would search for the variable by str_detect which returns a logical vector like grepl, and then collapse this by the base R function any which returns TRUE if there was at least one TRUE value.

dat %>% names %>% str_detect("d") %>% any()