loop for importation of data

How can I replace this

resul_D1<- base$resul_D1
resul_D2<- base$resul_D2
resul_D3<- base$resul_D3
resul_D4<- base$resul_D4
resul_D5<- base$resul_D5
resul_D6<- base$resul_D6
resul_D7<- base$resul_D7

by something like this

for ( i in 1:7){
  VarNom <- paste0("resul_D", i)
  VarNom <-base[[VarNom]]
}

Thank you for your time

Hello,

Could you be a bit more specific on the content of the data in each result?
Are these data frames you like to paste together? And where does the base variable from from, is this read from a file (or multiple?)

If you can show in some more detail what the input looks like and what you like the output to be, that would help. you can read the reprex guide for details on sharing data and code.
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:

Good luck
PJ

I just want to import variables (from resul_D1 to resul_D7) of a same database (base) as variable out of the database. I want to do a loop to avoid to repete seven lines.
I used to post reprex but in that case, i can't.

Are you looking for something like this?

base <- data.frame(resul_D1 = 1:4,
                   resul_D2 = 2:5,
                   resul_D3 = 3:6,
                   resul_D4 = 4:7)
for(i in 1:4) {
  NM <- paste0("resul_D", i)
  assign(NM, base[[NM]])
}
resul_D1
#> [1] 1 2 3 4

Created on 2022-04-05 by the reprex package (v2.0.1)

Yes exactly! Thank you! I am really not comfortable with loop in R. Have you any advices to master it? Indeed I have some other loops to do and I don't know how to adapt my code to it.
For example I tried this :

for ( i in 1:maxTest){
  VarNom <- paste0("resulD", i)
  base[[VarNom]]<-gsub("NA",0,base[[VarNom]])  
}

Instead of this

base$resulD1<-gsub("NA",0,base$resulD1)
base$resulD2<-gsub("NA",0,base$resulD2)
base$resulD3<-gsub("NA",0,base$resulD3)
base$resulD4<-gsub("NA",0,base$resulD4)
base$resulD5<-gsub("NA",0,base$resulD5)
base$resulD6<-gsub("NA",0,base$resulD6)
base$resulD7<-gsub("NA",0,base$resulD7)

You are probably over emphasizing the use of for loops. We shouldn't have a full discussion in this thread but generally, for loops are less common in R than in some other languages. There are functions that allow you to iterate over vectors, lists and data frame without an explicit loop. For your example of replacing the text "NA" with "0", you can use the functions from the dplyr package. mutate() changes or adds columns to a data frame and across() allows you to choose which columns are affected and what function is applied. In the code below, I replace "NA" with "0" in every column and then I change all of the columns to be numeric. A good place to learn about this sort of code is this book

https://r4ds.had.co.nz/

library(dplyr)
base <- data.frame(resul_D1 = c(1,2, "NA", 4),
                    resul_D2 = c("NA",2, "NA", 4),
                    resul_D3 = c(1,"NA", 5, 4),
                    resul_D4 = c(1,2, "NA", "NA"))
base
  resul_D1 resul_D2 resul_D3 resul_D4
1        1       NA        1        1
2        2        2       NA        2
3       NA       NA        5       NA
4        4        4        4       NA
base <- base |> mutate(across(.cols = everything(), .fns = ~gsub("NA", "0", .x)),
                       across(.cols = everything(), .fns = ~as.numeric(.x)))
base
  resul_D1 resul_D2 resul_D3 resul_D4
1        1        0        1        1
2        2        2        0        2
3        0        0        5        0
4        4        4        4        0

I agree that "for" function are not the best in R.
Thank you so much for this! I will study that!

This topic was automatically closed 7 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.