How to upload multiple CSV files correctly?

I have multiple CSV files to upload to RStudio. Based on Google search, I have found many ways to upload these files. I want all the csv files to be in one data frame and change some column type to char. I am trying method below:


myfiles = list.files(path="~/csvreports", pattern="*.csv", full.names=TRUE), lapply(myfiles, 
                      function(x) read.csv
                      (x, col_types = cols(`Plugin ID` = col_character(),
                                            CVSS = col_character(),
                                            Port = col_character())))

However I get this error:

Error in read.table(file = file, header = header, sep = sep, quote = quote, :
unused argument (col_types = list(cols = list(Plugin ID = list(), CVSS = list(), Port = list()),
default = list()))

My question other than how to fix this is, is this the best method to achieve what I wanted?

Is it better to have it in one data frame or each file as one data frame?


read.csv does not have a argument called col_types. It has one named colClasses.

Most probably you wanted to use read_csv instead of read.csv.

Since you're using tidyverse, probably you'll be better off with bind_rows. Also, if you want to use the tidyverse functions only, you can use map instead of lapply.

1 Like

Thanks Yarnabrina.

When I changed it to read_csv it works.

Why is map better than lapply?

In terms of performance there is not much difference, but since you are loading tidyverse purrr::map() is part of that family of packages, so integrates easily with other tidyverse packages, it works well with piped (%>%) commands, it has consistent parameters and the output type is predictable (from its variants e.g. map_dbl, map_int, etc.) and consistent.

1 Like

Thanks Andresrcs.

The reason I am using tidyverse is the R e-book I am reading right now is using it. I welcome if there is a better way to do this. It just that my knowledge of R is still very limited so if the codes seem to produce what I need, I am ok with it.

2 posts were split to a new topic: Help with filtering dataset - string matching

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