Reading multiple csv files

淺唱寂寞╮ 提交于 2021-02-17 05:40:28

问题


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)

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/57336293/reading-multiple-csv-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!