-1

I'm trying to create some objects inside a loop. I'm using a loop because I want my objects' names to be same as the files in the data folder. The problem is that the loop just keeps the last one:

feather_files <- list.files("../R",pattern="\\.feather$")

for (i in length(feather_files)) {

  assign(feather_files[i],read_feather(paste("../R/",feather_files[i],sep="")))

}

Does anybody know what I'm doing wrong?

DaveArmstrong
  • 11,680
  • 1
  • 10
  • 21
RSK
  • 1
  • 2
    Try `for (i in 1:length(featherfiles))`. Or, `for (file in featherfiles)` – jlhoward May 26 '22 at 06:43
  • 1
    You're also creating R objects whose identifiers are file names. That'sprobably going to cause problems later on. I'd recommend using `lapply` and assigning to a different object. If you populate `feather_files` with a call to `list.files` that includes `full.names=TRUE`, then `contents – Limey May 26 '22 at 06:59
  • Thanks a lot, that was a rookie mistake. – RSK May 26 '22 at 07:06
  • Also, see the answer to [this post](https://stackoverflow.com/questions/26235825/for-loop-only-adds-the-final-ggplot-layer) (Also by @jlhoward) explaining why you get the assignment from only the last evaluation of the loop. – Limey May 26 '22 at 08:15

0 Answers0