Working on existing variables dynamically in for-loop

I have 6 data frames in the global environment which I created thus:

for (i in 1:6){
  assign(paste0("bestpredX",i), 
allfits[allfits$iteration == apply(results.dat, 2, which.min)[i], 
                                          c("X_var", "Y_var", paste0("X",i), "iteration")])  
} 

Which returns a data frame of this format:

     X_var Y_var     X1 iteration
2829  69   151 135.8836        29
2830  64   131 140.3848        29
2831  67   129 137.6841        29
<...>

Then, I want to perform the same changes on each of these dfs (truncated for simplicity):

# Add a column 'Polynomial' and fill it all with factor Xi
bestpredX1$Polynomial <- as.factor("X1")
bestpredX2$Polynomial <- as.factor("X2")
<...>
bestpredX6$Polynomial <- as.factor("X6")
# Rename the 3rd column (Xi) to y.fit
names(bestpredX1)[names(bestpredX1) == "X1"] <- "y.fit"
names(bestpredX2)[names(bestpredX2) == "X2"] <- "y.fit"
<...>
names(bestpredX6)[names(bestpredX6) == "X6"] <- "y.fit"

I am not able to perform these in a for-loop using get(paste0("bestpredX",i)) , from what I understand it's because the variables already exist in the global environment. How can I go about for-looping operations on existing variables dynamically?

Note: In the present case I presume there might be a way to do all these even in the first for-loop. But I would like to learn how to perform it on existing variables; it's a task I encounter often.

Thanks!

Lacking a reprex see purrr:map as the preferred way to apply functions to a collection of objects.

My bad. I added some details to explain the goal. Perhaps a dummy df would suffice for reprex since the operations only deal with column names & adding new column?

# repeat for X1 to X6
bestpredX1 <- data.frame(matrix(1:20,
                         ncol = 4,
                         nrow = 5))
colnames(bestpredX1) <- c("X_var", "Y_var", "X1", "iteration")
1 Like
suppressPackageStartupMessages(library(purrr))

paste0("X",seq(1:6)) -> the_factors 

mk_frame <- function(X) {
  the_frame <- data.frame(matrix(1:20,
                                 ncol = 4,
                                 nrow = 5))
  colnames(the_frame) <- c("X_var", "Y_var", X, "iteration")
  the_frame$poly <- as.factor(X)
  return(the_frame)
}

map(the_factors,mk_frame) -> list_of_dat 

list_of_dat[[1]]
#>   X_var Y_var X1 iteration poly
#> 1     1     6 11        16   X1
#> 2     2     7 12        17   X1
#> 3     3     8 13        18   X1
#> 4     4     9 14        19   X1
#> 5     5    10 15        20   X1
list_of_dat[[2]]
#>   X_var Y_var X2 iteration poly
#> 1     1     6 11        16   X2
#> 2     2     7 12        17   X2
#> 3     3     8 13        18   X2
#> 4     4     9 14        19   X2
#> 5     5    10 15        20   X2

Created on 2020-09-12 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.