How to Fix the R Error: Undefined Columns Selected

Hey there, fellow R programmer! Have you ever encountered the dreaded “undefined columns selected” error message in your code? If so, you’re not alone. This error message can be frustrating and confusing, especially if you’re new to R or working with large datasets.

But fear not! In this article, we’ll take a deep dive into what causes this error message, how to interpret it, and most importantly, how to fix it. We’ll cover some common scenarios where this error might occur, and provide practical tips and tricks for resolving it. By the end of this article, you’ll be equipped with the knowledge and skills to tackle the “undefined columns selected” error like a pro. So let’s get started!

Why You’re Seeing the “Undefined Columns Selected” Error

In general, the “undefined columns selected” error occurs when R is unable to find the specific column name you’re trying to select, and therefore cannot perform the requested operation. Here’s an example of some code which triggers this error:

# create a data frame with three columns
my_df <- data.frame(
  character = c("Homer", "Marge", "Bart"),
  show = c("The Simpsons", "The Simpsons", "The Simpsons"),
  network = c("Fox", "Fox", "Fox")
)

# try to select a non-existent column name from a subset of the data frame
subset_df <- subset(my_df, show == "Family Guy")
result <- subset_df[, c("character", "catchphrase")]

When we run this, we get the following error…

Error in `[.data.frame`(subset_df, , c("character", "catchphrase")) : 
  undefined columns selected

Which makes sense. We never defined a field named catchphrase.

The Usual Suspects: How To Fix R’s “Undefined Columns Selected” Error

The real world causes of this can be a little more complex, although they usually point towards human error….

  • You misspelled the name of the column(s) you’re trying to select.
  • You’re using the wrong syntax to select columns (e.g., using parentheses instead of square brackets).
  • The column(s) you’re trying to select were not included in the original data frame or have been removed by another step in your process
  • An error in a data load process left a missing value in the wrong place

In any event, the right thing to do here is to first check your syntax for errors. If that doesn’t find the issue, start tracing through your program step by step, to confirm the field you’re asking for actually exists. You can use the head () function to peak inside a data frame.

Other Ways to Trigger and Fix R’s “Undefined Columns Selected” Error

In addition to syntax snafu’s, there are some advanced ways to trigger the “Undefined Columns Selected” error in your R code.

Unlike certain other languages, R tends to be pretty literal when comparing two values. If the data type used for the column names within the data frame doesn’t match the data type you’re using for your reference variable, R will usually throw an error. From personal experience, attempting to compare a string data type and an object data type can generate these kind of errors and can easily occur when you’re trying to bolt together the results of several different R package(s) . While unlikely if you’re sticking with simple base R code (text column names), this failure mode can come out to play if you’re getting creative about code generation or abstraction. You can use the str() function to inspect the data types, since it may be hard to spot the difference visually on the console output.

Non-standard evaluation (NSE) functions in R are another potential candidate for generating reference errors such as this. These are a collection of functions in dplyr and ggplot2 r package(s) which use non-traditional approaches to their syntax to enable a more concise and flexible syntax for working with data frames and other data structures in R. Some examples of NSE functions in R include dplyr::select()ggplot2::aes(), and base::subset().

Unfortunately, this means NSE functions can also be more difficult to debug and can lead to errors if you’re not familiar with their behavior. It’s important to read the documentation carefully and test your code thoroughly when using NSE functions in R. With great syntactical power, comes great responsibility….

Scroll to top
Privacy Policy