150

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.

d-cubed
  • 1,034
  • 5
  • 30
  • 58
Sunny Sunny
  • 3,204
  • 4
  • 25
  • 26
  • 1
    Do you want to know whether your data frame has a column with a name of `d`, or do you want to know whether a given vector `d` equals one of the columns of your data frame? –  Apr 23 '12 at 07:01
  • i want to knnow whether dataframe has a cloumn with name d or not – Sunny Sunny Apr 23 '12 at 07:02
  • Wish you nice Sunny day with 100 votes! :-) – Tomas Mar 25 '20 at 11:21

5 Answers5

256

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");
}
  • 11
    if you are looking the inverse, i.e. if the column is not there, just add `!` at the beginning: `if(!"d"%in% colnames(dat))` – toto_tico Jun 28 '16 at 13:47
  • Awesome answer. How do I extend this if I'd be looking for columns 'd' and 'e' and 'f'? Would that be: `if("d" & "e" & "f" %in% colnames(dat)) { cat("Yep, it's in there!\n"); }`. Thanks! -- Oh, I may have found the answer myself: http://stackoverflow.com/questions/21770912/how-to-check-if-a-column-exists-in-a-matrix-or-data-frame. – Sander W. van der Laan Nov 30 '16 at 09:38
  • 9
    all(c("d", "e", "f") %in% colnames(dat)) – skan Jul 28 '17 at 19:59
32

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
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • 11
    To get the `grepl` a bit more precise, you could use `grepl("^d$",names(dat))`, to ensure that a column with name `dd` does not return `TRUE`. – BenBarnes Apr 23 '12 at 08:09
  • Thanks for this, `colnames` didn't work for me but `names` did. – Docconcoct Dec 20 '17 at 10:35
  • @Andrie is there a way to compare the columns to two large dataframes to see which column names are missing from the other column? – sar Apr 03 '20 at 17:48
10

You could use any:

> names(dat)
[1] "a" "b" "c"
> any(names(dat) == 'b')
[1] TRUE
> any(names(dat) == 'B')
[1] FALSE
Tomasz Pik
  • 401
  • 3
  • 3
7

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")
}
dpel
  • 1,954
  • 1
  • 21
  • 31
Jackson
  • 3,555
  • 3
  • 34
  • 50
2

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()
Agile Bean
  • 6,437
  • 1
  • 45
  • 53