简体   繁体   中英

trouble with reading csv file in rbind, “rt” kind of mistake

I'm having some trouble getting the file from direcotry even though RStudio has already recognised the file

library(readr)
read.csv2(names)
list.files("C:\\Users\\consultor6\\Desktop\\PIBIC\\dados")

names = list.files("C:\\Users\\consultor6\\Desktop\\PIBIC\\dados")
lista = list()

lista[[1]] = read.csv2("201812_ESTBAN.CSV")

for(i in 2:length(names)) {

  lista[[i]] = read.csv2(names[i])

}

base = do.call(what = lista, args = rbind)

The error is the following:

library(readr) read.csv2(names) Error in file(file, "rt") : invalid 'description' argument list.files("C:\\Users\\consultor6\\Desktop\\PIBIC\\dados") [1] "200701_ESTBAN.CSV" "200702_ESTBAN.CSV" "200703_ESTBAN.CSV" "200704_ESTBAN.CSV" [5] "200705_ESTBAN.CSV" "200706_ESTBAN.CSV" "200707_ESTBAN.CSV" "200708_ESTBAN.CSV" [9] "200709_ESTBAN.CSV" "200710_ESTBAN.CSV" "200711_ESTBAN.CSV" "200712_ESTBAN.CSV" [13] "200801_ESTBAN.CSV" "200802_ESTBAN.CSV" "200803_ESTBAN.CSV" "200804_ESTBAN.CSV" [17] "200805_ESTBAN.CSV" "200806_ESTBAN.CSV" "200807_ESTBAN.CSV" "200808_ESTBAN.CSV" [21] "200809_ESTBAN.CSV" "200810_ESTBAN.CSV" "200811_ESTBAN.CSV" "200812_ESTBAN.CSV" [25] "200901_ESTBAN.CSV" "200902_ESTBAN.CSV" "200903_ESTBAN.CSV" "200904_ESTBAN.CSV" [29] "200905_ESTBAN.CSV" "200906_ESTBAN.CSV" "200907_ESTBAN.CSV" "200908_ESTBAN.CSV" [33] "200909_ESTBAN.CSV" "200910_ESTBAN.CSV" "200911_ESTBAN.CSV" "200912_ESTBAN.CSV" [37] "201001_ESTBAN.CSV" "201002_ESTBAN.CSV" "201003_ESTBAN.CSV" "201004_ESTBAN.CSV" [41] "20 1005_ESTBAN.CSV" "201006_ESTBAN.CSV" "201007_ESTBAN.CSV" "201008_ESTBAN.CSV" [45] "201009_ESTBAN.CSV" "201010_ESTBAN.CSV" "201011_ESTBAN.CSV" "201012_ESTBAN.CSV" [49] "201101_ESTBAN.CSV" "201102_ESTBAN.CSV" "201103_ESTBAN.CSV" "201104_ESTBAN.CSV" [53] "201105_ESTBAN.CSV" "201106_ESTBAN.CSV" "201107_ESTBAN.CSV" "201108_ESTBAN.CSV" [57] "201109_ESTBAN.CSV" "201110_ESTBAN.CSV" "201111_ESTBAN.CSV" "201112_ESTBAN.CSV" [61] "201201_ESTBAN.CSV" "201202_ESTBAN.CSV" "201203_ESTBAN.CSV" "201204_ESTBAN.CSV" [65] "201205_ESTBAN.CSV" "201206_ESTBAN.CSV" "201207_ESTBAN.CSV" "201208_ESTBAN.CSV" [69] "201209_ESTBAN.CSV" "201210_ESTBAN.CSV" "201211_ESTBAN.CSV" "201212_ESTBAN.CSV" [73] "201301_ESTBAN.CSV" "201302_ESTBAN.CSV" "201303_ESTBAN.CSV" "201304_ESTBAN.CSV" [77] "201305_ESTBAN.CSV" "201306_ESTBAN.CSV" "201307_ESTBAN.CSV" "201308_ESTBAN.CSV" [81] "201309_ESTBAN.CSV" "201310_ESTBAN.CSV" "201311_ESTBAN.CSV" "201312_ESTBAN.CSV" [85] "201401_ESTBAN.CSV" "201402_ESTBAN.CSV" "201403_ESTBAN.CSV" "201404_ ESTBAN.CSV" [89] "201405_ESTBAN.CSV" "201406_ESTBAN.CSV" "201407_ESTBAN.CSV" "201408_ESTBAN.CSV" [93] "201409_ESTBAN.CSV" "201410_ESTBAN.CSV" "201411_ESTBAN.CSV" "201412_ESTBAN.CSV" [97] "201501_ESTBAN.CSV" "201502_ESTBAN.CSV" "201503_ESTBAN.CSV" "201504_ESTBAN.CSV" [101] "201505_ESTBAN.CSV" "201506_ESTBAN.CSV" "201507_ESTBAN.CSV" "201508_ESTBAN.CSV" [105] "201509_ESTBAN.CSV" "201510_ESTBAN.CSV" "201511_ESTBAN.CSV" "201512_ESTBAN.CSV" [109] "201601_ESTBAN.CSV" "201603_ESTBAN.CSV" "201604_ESTBAN.CSV" "201605_ESTBAN.CSV" [113] "201606_ESTBAN.CSV" "201607_ESTBAN.CSV" "201608_ESTBAN.CSV" "201609_ESTBAN.CSV" [117] "201610_ESTBAN.CSV" "201611_ESTBAN.CSV" "201612_ESTBAN.CSV" "201701_ESTBAN.CSV" [121] "201702_ESTBAN.CSV" "201703_ESTBAN.CSV" "201704_ESTBAN.CSV" "201705_ESTBAN.CSV" [125] "201706_ESTBAN.CSV" "201707_ESTBAN.CSV" "201708_ESTBAN.CSV" "201709_ESTBAN.CSV" [129] "201710_ESTBAN.CSV" "201711_ESTBAN.CSV" "201712_ESTBAN.CSV" "201801_ESTBAN.CSV" [133] "201802_ESTBAN.CSV" "201803_ESTBAN.CSV" "201804_ESTBAN.CSV" "201805_ESTBAN.CSV" [137] "201806_ESTBAN.CSV" "201807_ESTBAN.CSV" "201808_ESTBAN.CSV" "201809_ESTBAN.CSV" [141] "201810_ESTBAN.CSV" "201811_ESTBAN.CSV" "201812_ESTBAN.CSV" names = list.files("C:\\Users\\consultor6\\Desktop\\PIBIC\\dados") lista = list()

lista[[1]] = read.csv2("201812_ESTBAN.CSV") Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file '201812_ESTBAN.CSV': No such file or directory

for(i in 2:length(names)) { +
+ lista[[i]] = read.csv2(names[i]) +
+ } Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file '200702_ESTBAN.CSV': No such file or directory

base = do.call(what = lista, args = rbind) Error in do.call(what = lista, args = rbind) : segundo argumento deve ser uma lista

I'm trying to stack the 143 excel files but need to drop the first three lines from matrixes 2 to 143

Your read.csv2 statements will only work if your working directory is set appropriately. At the moment, you're just passing the file name to the function, and if your working directory isn't the containing folder, it won't work.

The easiest solution here is to use full.names = TRUE in list.files which includes the full path to the file, relative to your current working directory, where applicable.

Changing the names definition to the following should fix the problem:

names = list.files("C:\\Users\\consultor6\\Desktop\\PIBIC\\dados", full.names = TRUE)

Your files are in the directory "C:\\\\Users\\\\consultor6\\\\Desktop\\\\PIBIC\\\\dados" and are correctly found by list.files() .

However, when you try to read in the files, you forgot to add this path:

lista[[1]] = read.csv2("201812_ESTBAN.CSV")

Hence read.csv2 looks for the samples in your current working directory and can not find them (you can check the working directory with getwd() ).

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