Leonardo Januario
Leonardo Januario

Reputation: 45

How can I get the column names that are resulting in the "undefined columns selected" error?

I got this error in a piece of code where I subset a data frame using a list with the columns I wish to mantain. I have and managed to find the problematic column (column name was mispelled in the list) and solved the problem.

But to accomplish that, I had to check each entry in the list, checking column names one by one to find it.

Is there a way (some function, perhaps) to make R show what columns are undefined?

Upvotes: 1

Views: 84

Answers (1)

TarJae
TarJae

Reputation: 78917

You may try purrr functions map_depth and vec_depth. Learned here Extract colnames from a nested list of data.frames. Credits to Ronak Shah.

library(purrr)

return_names <- function(x) {
   if(inherits(x, "list"))
     return(map_depth(x, vec_depth(x) - 2, names))
    else return(names(x))
}

map(yourlist, return_names)

Upvotes: 1

Related Questions