0

I need to read multiple csv files and print the first six values for each of these. I tried this code but it is obviously wrong because the value of di is overwritten each iteration of the loop. How can I read multiple files?

library(xlsx)
for(i in 1:7){
    di = read.csv(file.choose(),header=T)
    print(di)
}
d = list(d1,d2,d3,d4,d5,d6,d7)
lapply(d,head)
Til
  • 5,150
  • 13
  • 26
  • 34
th_cy
  • 51
  • 1
  • 4
  • You can use the list.files function: https://stat.ethz.ch/R-manual/R-devel/library/base/html/list.files.html – nv_wu Aug 03 '19 at 07:50
  • Possible duplicate of [How to import multiple .csv files at once?](https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once) – paqmo Aug 03 '19 at 13:06

2 Answers2

0

I'm slightly confused by if you want to just print the 6 lines or store them and if you want to keep the remainder of the csv files or not. Let's say all you want is to print the 6 lines, then, assuming you know the file names you can do this with

print(read.csv(filename, nlines = 6))

And repeat for each file. Alternatively if you want to save each file then you could do

f1 <- read.csv(filename, nlines = 6)

Repeat for each and use print(head).

Alternatively using your method but fixing the overwrite problem:

library(xlsx)
for(i in 1:7)
  assign(paste0("d",i), read.csv(file.choose(),header=T))

  lapply(list(d1,d2,d3,d4,d5,d6,d7),head)

The use of assign dynamically assigns names so that each is unique and doesn't overwrite each other, which I think is what you were aiming for. This isn't very 'elegant' though but fits with your chosen method

RaphaelS
  • 839
  • 4
  • 14
  • I want to read 7 csv files and also save each of them. I wanted to know if there is way of doing it without writing read.csv statement 7 times. Because if there are several files it will be tedious to write a read statement for each one of them. – th_cy Aug 03 '19 at 08:06
  • If they are all in one directory then you could do: `x <- as.list(list.files(pattern = "csv"))` `for(i in 1:length(x))` `assign(paste0("d",i), read.csv(x[[i]], header=T))` – RaphaelS Aug 03 '19 at 12:05
0

If you want to keep you data frames in a list, rather than assigning each to a new object.

Option 1:

fs <- dir(pattern = ".csv")
d <- list()
for (i in seq_along(fs)) {
    d[[i]] <- read.csv(fs[[1]])
    print(head(d[[i]]))
    }

Option 2:

fs <- dir(pattern = ".csv")
d <- lapply(fs, read.csv)
lapply(d, head)

Using option 1, you need to initialize an empty list to populate and assign with double bracket [[ notation. Using option 2, you don't need to initialize an empty list.

paqmo
  • 3,649
  • 1
  • 11
  • 21