Reading and combining multiple .csv files (delim= ";") from a folder into R

Hello everyone,

I hope you do not mind helping me with this likely quite simple problem I am having.
I am trying to read multiple .csv files within a folder into R and combine these files into one longer file. I have done some research and found that there are a bunch of different ways to successfully do this! However, I am running into one small problem: The .csv files that I am working with are use semicolons to separate data rather than using a comma as usual.

When I am running my code, I am therefore having a problem reading in these data files.

I am hoping someone doesn't mind helping with this .

Below I have included what is my current, unsuccessful code. Here you can see that I am trying to read in multiple files (a number of files that will increase weekly). But do not know how to take into consideration the ";" separator in my .csv files.

'''

 folderfiles <- list.files("PATH", pattern = "*.csv", full.names = TRUE)
 data_csv <- ldply(folderfiles, read_csv)

'''

Note: I have also tried the following unsuccessfully:

'''

 folderfiles <- list.files("PATH", pattern = "*.csv", full.names = TRUE)
 data_csv <- ldply(folderfiles, read_delim(delim = ";"))

'''

1 Like

readr::read_delim("your.csv", delim = ';')

1 Like

Thank you for your help. This however does not solve my issue. My issue is that when automatically reading in multiple .csv files into R, I am unsure how to take into account the delim=";" statement. I will edit my original post with my current, unsuccessful code.

Two possibilities library(readr) has not been run and read_delim hasn't been called with readr::read_delim.

Putting failing code into a reprex will help. See the FAQ: How to do a minimal reproducible example reprex for beginners.

1 Like

The error code that I get when I run the code you have referenced in this comment is the following:

"Error in read_delimited(file, tokenizer, col_names = col_names, col_types = col_types, :
argument "file" is missing, with no default"

Thank you for your feedback so far

I am unfamiliar with the reprex function but I ran it as you suggested and it says the following:
"``` r
data_csv <- ldply(folderfiles, read_delim(delim = ";"))
#> Error in ldply(folderfiles, read_delim(delim = ";")): could not find function "ldply"


<sup>Created on 2021-05-08 by the [reprex package](https://reprex.tidyverse.org) (v2.0.0)</sup>"

For the first, you should use the quoted name of the file ; and for the second you need the plyr library loaded. Once you have it working for a single file, come back to do multiple.

Thanks for the help. I must be missing something here because this is an easy task to read one file. however I am still confused regarding what will make this a successful code. I will continue researching.

Now that you can read one file, you will want to use a function that takes a list of file names (perhaps a list returned from another function) and sends them to a function that reads the single file, replacing the hard-coded file name with a variable designation.

There are a number of ways to do this. If you are already using {tidyverse} packages, I'd start with {purrr}. Otherwise there are Map lapply and for.

Obviously, I can't test this since I don't have access to your files but, for reading and combining multiple files, the code should look like this:

library(tidyverse)

folderfiles <- list.files(path = "YOUR_FILE_PATH",
                          pattern = "\\.csv$",
                          full.names = TRUE)

data_csv <- folderfiles %>% 
    set_names() %>% 
    map_dfr(.f = read_delim,
            delim = ";",
            .id = "file_name")
3 Likes

Thank you for the help everyone. @andresrcs this is an excellent solution!
I have also found that the read_bulk function in the readbulk package is successful as well!

Thank you once again

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.