R error in data.frame : undefined columns selected

For me, R error undefined columns selected appears when I try to select some of the columns to keep in the data frame. It is easy to solve. You have to search for the misspelled column name, but if there is a long list of column names, then here is how to do that quickly.

Here is an example with the R built-in dataset Iris. If I’m trying to select columns to keep by column names with typos, then there are undefined columns selected error.

keep <- c("Sepal.Lenght", "Petal.Lenght", "Species")

iris_sm <- iris[keep]

The next thing is the mismatch detection. Here is an example of how to subset in R all column names from the list.

         match(keep, names(iris))

In case of a lengthy list of column names, this might be good enough.


If you want to know how to reflow your code or other useful RStudio tips and tricks, take a look at this post.

1 comment on “R error in data.frame : undefined columns selected

  1. YES!!! This is amazing. Solved my problem – thank you so much!

Comments are closed.