user12027316
user12027316

Reputation: 123

R For-Loop isn't reading in .csv files correctly

I'm having an issue with my R code where I get this error:

Error in read.table(file = file, header = header, sep = sep, quote = quote, : 'file' must be a character string or connection

I've read possible issues that occur because of this error but they don't apply so I'm wondering, is it a library function? Someone also keeps telling me it's an issue with the setwd but the working directory is 100% correct. Is it just the way the for-loop is working on multiple .csv?


---
title: "CRS PPC 3.11.22"
output:
  word_document: default
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(readxl)
library(tidyr)
library(dplyr)
library(openxlsx)
library(dplyr)
library(tidyr)
library(readr)
library(lubridate)
library(data.table)
library(corrgram)
library(corrplot)
library(ggplot2)

#read files

setwd("[directory location]")

file.list <- list.files(pattern='*.csv')

Data <-list()

for(i in 1:length(file.list)){Data[[i]]<-data.frame(read.csv(file.list[i]))}

Here is the full output for that code:

> 
> library(readxl)
> library(tidyr)
> library(dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

> library(openxlsx)
> library(dplyr)
> library(tidyr)
> library(readr)
> library(lubridate)

Attaching package: ‘lubridate’

The following objects are masked from ‘package:base’:

    date, intersect, setdiff, union

> library(data.table)
Registered S3 method overwritten by 'data.table':
  method           from
  print.data.table     
data.table 1.14.2 using 10 threads (see ?getDTthreads).  Latest news: r-datatable.com

Attaching package: ‘data.table’

The following objects are masked from ‘package:lubridate’:

    hour, isoweek, mday, minute, month, quarter,
    second, wday, week, yday, year

The following objects are masked from ‘package:dplyr’:

    between, first, last

> library(corrgram)
> library(corrplot)
corrplot 0.92 loaded
> library(ggplot2)
> 
> #read files
> 
> setwd("[directory location]")
> 
> file.list <- list.files(pattern='*.csv')
> 
> Data <-list()
> 
> for(i in 1:length(file.list)){Data[[i]]<-data.frame(read.csv(file.list[i]))}
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  'file' must be a character string or connection

Upvotes: 1

Views: 333

Answers (2)

Christoph
Christoph

Reputation: 7063

The following works in my case:

path <- "C:/your path to your files"
file.list <- list.files(path = path, pattern = "*.csv")

data <- list()
for(i in 1:length(file.list)){
  data[[i]] <- read.csv(file = paste0(path, "/",file.list[[i]]))
}

If you compare with your code, there seem to be some minor issues.

I would also not use setwd() in scripts. Just use the (full) paths.

Upvotes: 1

zephryl
zephryl

Reputation: 17069

This may be your problem:

file.list <- list.files(pattern='*.csv')

pattern should be a regular expression, and "*" isn’t a wildcard in regex. Instead try pattern = 'csv$' to match files ending with "csv".

Upvotes: 1

Related Questions