I have a list of CSV URLs from the net and have merged them to a vector.
Now, I want to read this list with read_csv
.
Example:
files <- c("csv_link1.csv",
"csv_link2.csv",
"csv_link3.csv",
and so on....)
data <- map_dfr(files, read_csv)
This is no problem. The Problem is that in the CSV files there are columns that are filled with different values. So, for example, in CSV1 there is column "V1", which is filled with double and in CSV the same column is "V1", filled with characters. Merging the CSVs does not work because they are different data types.
In my case, I think there are two possibilities to solve this.
- I only import certain columns, so I say
read_csv
only reads column (V2 and V3) but not V1
Or
- I merge the columns to the same data type with
col_types
I have tried both, but failed because of the correct syntax.
I tried something like
data <- map_dfr(files, read_csv(cols_only(the col names)))
But, this don't work.
How can I import and merge only specific columns?
Concrete Example in my case:
library(data.table)
library(readr)
library(purrr)
files <- c("https://www.football-data.co.uk/mmz4281/1920/EC.csv",
"https://www.football-data.co.uk/mmz4281/1819/EC.csv",
"https://www.football-data.co.uk/mmz4281/1718/EC.csv",
"https://www.football-data.co.uk/mmz4281/1617/EC.csv",
"https://www.football-data.co.uk/mmz4281/1516/EC.csv",
"https://www.football-data.co.uk/mmz4281/1415/EC.csv",
"https://www.football-data.co.uk/mmz4281/1314/EC.csv",
"https://www.football-data.co.uk/mmz4281/1213/EC.csv",
"https://www.football-data.co.uk/mmz4281/1112/EC.csv",
"https://www.football-data.co.uk/mmz4281/1011/EC.csv")
data <- map_dfr(files, read_csv)
Error: Can't combine `BbAH` <character> and `BbAH` <double>.
So i the column BbAH
has different data types. But i dont need this column.
It would be cool if i can chose the columns which would be merged befor the merge run into an error because of this different data type problem.