To find whether a column exists in data frame or not Ask Question

To find whether a column exists in data frame or not Ask Question

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.

ベストアンサー1

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");
}

おすすめ記事