简体   繁体   中英

Reading multiple csv files

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)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM