ERROR: Undefined columns selected

Hi everyone, I'm trying to do a loop, but I have this error: "ERROR:undefined columns selected". I dont know how to solve it, any help will be appreciated.
species_vector <- c("QCall","PLall","RLall")
species_name <- c("Quercus_coccifera","Pistacia_lentiscus","Rhamnus_lycioides")

for (i in c(1:3))
for (k in c(1:3))
species_vector_i <- species_vector[i]
species_name_k <- species_name[k]
srw <- read.rwl(paste0("C:/Users/user/Desktop/Uni/TFG/Análisis/A1 (Datos de crecimiento)/",species_vector_i,".rwl"))
srw <- data.frame(srw, stringsAsFactors = FALSE )
microsite <- c("1","2","3","4")
for (j in c(1:4))
microsite_j <- microsite[j]
microsite_series <- metadata[metadata$especie==species_name_k &
srw_j <- srw[,c(microsite_series)]
srw_jcol <- ncol(srw_j)
srw_j[,2:srw_jcol] <- sapply(srw_j[,2:srw_jcol],as.numeric)
srw_j$mean <- apply(srw_j[,2:srw_jcol], 1,mean, na.rm = TRUE)
srw_j$sd <- apply(srw_j[,2:srw_jcol], 1, sd, na.rm = TRUE)
srw_j$count_series <- apply(srw_j[,2:srw_jcol], 1, function(x) sum(!
srw_j$sdsup <- srw_j$mean + srw_j$sd
srw_j$sdinf <- srw_j$mean - srw_j$sd
srw_j$year <- rownames(srw_j)
assign(paste0(species_vector_i,microsite_j,"_srw"), srw_j)
}, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})


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,