vog
vog

Reputation: 836

Loop character variable and interact it to create names in dplyr

I want to loop through the names of a character variable of this type:

names_list <- c("USA", "VEN", "CHE")

Then, I create the following loop:

for (i in names_list) {
  set.seed(123333)
  test <- predictors_no_NA %>%
    filter(ISO3_baci != i)

predictions <- predict(rf150, test, predict.all=TRUE, type = "prob")
  predictions <- as.data.frame(predictions[1])
  predictions <- predictions %>%
    select(aggregate.1) %>%
    rename(predictions[i] = aggregate.1)    % HERE APPEARS THE 1st PROBLEM

  test_RF_[i] <- cbind(test, predictions)   % HERE APPEARS THE 2nd PROBLEM
}

Note that variable "rf150" is created inside the loop without any problem (don't show the code here).

The problem arises when I want to add the string element of the loop (e.g. "USA") to my created name "predictions_[i]" or "test_RF_[i]", so that I can get a variable that is called: "predictions_USA" or "test_RF_USA" as well as "predictions_VEN" or "test_RF_VEN" and "predictions_CHE" or "test_RF_CHE"

Any clue?

Regards

Upvotes: 0

Views: 295

Answers (1)

alex_jwb90
alex_jwb90

Reputation: 1723

Firstly, for the dynamic renaming in your loop, the following snippet should help (I've taken the freedom to alter it a little:

library(tibble) # if you want to use as_tibble() rather than as.data.frame()
library(dplyr)

predictions <- predict(rf150, test, predict.all=TRUE, type = "prob") %>%
    .[1] %>%
    as_tibble() %>%
    select(!!paste0('predictions_',i) := aggregate.1)

There's a very helpful and detailed answer on the topic of dynamic variable names in dplyr over here.

Alternative solution, since you had already selected only the one column that you wanted to colbind:

predictions  <- [...] %>%
    select(aggregate.1) %>%
    `colnames<-`(paste0('predictions_',i))

Secondly, to assign the data frame to a variable, as commented by Santiago above, assign should get you there:

assign(
    paste0('test_RF_',i),
    cbind(test, predictions),
    envir=globalenv()
)

Upvotes: 1

Related Questions