Home > Enterprise >  assign to the index of the loop content not working in R
assign to the index of the loop content not working in R

Time:01-08

I am trying to refactor my code instead of creating a loop for every element in dir_list. I need to create a new dataframe and assign the name to it according to the content in dir_list as I loop through it. But it gives me the error "Error in paste(i) <- AllData : could not find function "paste<-" which I do not understand.

for (i in filesNames) {
  for (j in dir_list) {
      
        data <- read_table(paste(workDir,j, "/", i ,".txt", sep = ""))
        data <- data %>% mutate(quarter = j)
        if (j == dir_list[1]) {
          AllData <- data 
        } else {
          AllData <- bind_rows(AllData, data)
        } 
        if (j ==  dir_list[length(dir_list)]) {
          paste(i) <-  AllData;
        }
  }
}  

CodePudding user response:

Why would you assing the dateframe to i?
If that is your intention, use assign(i, AllData)

  •  Tags:  
  • Related