'Row bind csv files with different data types of a column

I have a folder containing multiple csv files. The data type of one of the column(suppose is "column1") in each csv may be different. I plan to use the following code to bind these csv files into a data.frame:

library(dplyr)
library(readr)
df_list<- list.files(path = ".\\csv_folder",pattern="*.csv",full.names=T )
df <- do.call(bind_rows, lapply(df_list,function(x) read_csv(x)))

but I get the following error:

Error: Can't combine `column1` <double> and `column1` <character>.

How do I unify the data type of column1 and bind the csv files?

Many Thanks



Solution 1:[1]

It might be useful to insert a bit of your data in the question so we can see what you're dealing with. You can do so by using the following code

dput(head(mydata, x)) #where x is the number of rows you want for your sample, always try to keep it small

Without a sample of your data I can't see what the problem is, but it seems that the columns you're trying to bind are of different classes (double categories store numbers with two decimal digits, while a character variable it's stored as text).

Depending on what's best for you, you should try to change the class of one of those columns. Perhaps both are numeric but one of them is stored as a character column, in which case you should use

mydata$column1<-as.numeric(mydata$column1)

In case it's the other way round, you just gotta use as.character instead.

Hope it works!!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nico