0

I'm trying to import the csv files from my working directory. There are 3 such files, but for some reason R insists on recognizing only one of them. I can't determine what the pattern is, and if the recognized file is moved out of the folder then nothing is recognized. Here is my code:

files = list.files(pattern="*\\.csv$")

Each of the files is for sure a csv file which I confirmed by inspecting the "Type" column in the windows folder navigator, and to be safe I also saved a copy as CSV and still had the same problem.

Is there an aspect to this I'm unaware of?

Thanks!

Rookatu
  • 1,487
  • 3
  • 21
  • 50
  • Try `files = list.files(pattern=".csv")` – Kevin Jul 22 '17 at 01:32
  • Hey @Kevin. I had tried that with no effect. I've tried several permutations of the regex pattern, none of which worked. Any ideas? – Rookatu Jul 22 '17 at 01:33
  • Use `getwd()` and make sure the working directory is pointing to the directory with the files. The above works properly. Also this only gets the file names it does not read them. – Kevin Jul 22 '17 at 01:34
  • Hey. I know that I'm in the right working directory because it does recognize one of the files in that directory (I also set it manually). And I'm only looking for the names here. – Rookatu Jul 22 '17 at 01:38
  • The above works assuming all of the files names actually end in ".csv" exactly. The `files` variable should have all three listed. > files <- list.files(pattern = ".csv") > files [1] "CopyOfdiamonds.csv" "diamonds.csv" > – Kevin Jul 22 '17 at 01:42
  • All the files end in .csv as I've mentioned. Additionally, if I use the function `read.csv()` on one of the files which wasn't recognized by `files.list()`, I am able to read it in (and I use ".csv" when specifying the filename with `read.csv()` by the way). Therefore something else must be preventing this from working. – Rookatu Jul 22 '17 at 01:46
  • try just `list.files()` and see what is returned. – Kevin Jul 22 '17 at 01:50
  • When I do this I see all the files, and I see that the difference between the file extensions is case (i.e. ".CSV" vs ".csv"). I guess I need to modify the regex accordingly with a "?i" somewhere? – Rookatu Jul 22 '17 at 01:52
  • The case matters. – Kevin Jul 22 '17 at 02:00
  • 1
    `list.files` does have a parameter `ignore.case`, set that to `TRUE` – rosscova Jul 22 '17 at 03:45
  • Have you tried the alternative `choose.files()`? It's meant to choose files interactively in Windows. – Rui Barradas Jul 22 '17 at 09:40
  • try this please files `files<- list.files(pattern=".*.csv")` – Orhan Yazar Jul 22 '17 at 10:10
  • I had used the `ignore.case` and that did work. I'm curious as to why some of the file extensions were capitalized and some were not, but this detail was not displayed by the file type in windows explorer. – Rookatu Jul 22 '17 at 13:17

1 Answers1

2

The issue turned out to be that the file extension for the file that worked was ".csv" and for the ones that didn't was ".CSV". I do not know how or why something like that can happen, but the pattern parameter of the list.files function is case sensitive.

Using the parameter setting ignore.case = TRUE solved this issue.

Rookatu
  • 1,487
  • 3
  • 21
  • 50