ERROR: Undefined columns selected


Welcome to the RStudio community!

First of all, let me show you a link to a page where you can see how to build a reprex. A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:

With this, it will be much more likely that someone actually is going to answer your questions, instead of having to comb through a mess of code and a screenshot :slight_smile:

That said, although I could not run your code (no reprex), I know what might cause this type of error. It occurs when you are subsetting a data frame with invalid actions. Here is an overview of some cases in which this might happen:

# Dummy Data
myData = data.frame(
  x =1:5, 
  y = LETTERS[1:5]

# 1) Forgetting to define both the rows and columns needed
#> Error in `[.data.frame`(myData, 1:10): undefined columns selected

# 2) Asking for more columns than exist
#> Error in `[.data.frame`(myData, 1, 1:7): undefined columns selected

# 3) Subsetting with NA
myColumn = NA
myData[1, myColumn]
#> Error in `[.data.frame`(myData, 1, myColumn): undefined columns selected

Created on 2021-04-23 by the reprex package (v2.0.0)

In your code, I can see that in line 375-378 (judging by the screenshot) there is code that subsets the data frame and thus where the error might occur. If this is occurring in a loop only at a certain point when you run the loop and it breaks, the iteration variable (e.g. i, or j) will hold the value of the last iteration when the error occured. You can rerun the inside of the loop line by line until it occurs again and debug.

If this fixes the issue, great! If not, please reads the reprex guide and try again :slight_smile:

Hope this helps,