0

I have 20 files named 1.csv, 2.csv, 3.csv etc, which I want to read into R and merge using rbind using a loop. I've tried the code below but I get an error message saying unexpected "[" in the second line.

for (i in 1:22) {
  fish[i]  <- read.csv([i].csv)
  combined <- rbind(fish[i], fish[i+1])
}
jogo
  • 12,469
  • 11
  • 37
  • 42
Alice
  • 1
  • 1

1 Answers1

0

To put my comment in a answer:

# just for rbindlist, there are also base ways to do this
library(data.table)

fish <- list()
for (i in 1:20) {
  fish[[i]] <- read.csv(paste0(i, ".csv"))
}
combined <- rbindlist(fish)

Alternative: Same as do.call("rbind", fish) on data.frames, but much faster.

Jakob Gepp
  • 463
  • 3
  • 10