0

I'm using the below syntax to gather all files in a folder, read them, and merge based on common columns.

allData <- list.files(path="/Users/me/Desktop/Holder", full.names = TRUE) %>%
  lapply(read_csv) %>%
  bind_rows

However, in some of my files, a certain column has NAs. Thus, it won't merge and I get the following error:

Error: Can't combine `..1$D_postIAT` <double> and `..2$D_postIAT` <character>.

How can I get around this?

...I tried to create an example with syntax, but it's hard for me to reproduce:

a<-t(c(123, 456, 789))
a<-as.data.frame(a)
b<-t(c(987, NA, 654))
b<-as.data.frame(b)
#b[1,2]<-as.logical(b[1,2])
b[1,2]<-as.character(b[1,2])

^I first tried to make the NA a logical value, but that didn't work. I could make it a character though.

c<-as.list(c(a,b))
bind_rows(c)

^the bind_rows didn't work, because the columns had the same name, but I thought that's how it knows what to bind?

d<-NA
as.data.frame(d)
b[1,2]<-d

^When I tried to create a logical vlaue for NA, it converted back to numeric after I added it to b.

j400nb
  • 17
  • 5
  • it might not have to do with NA but rather one column is a character and one is a numeric field, but to further help we need to see what the data looks like in both sets of data. – Mike Feb 01 '22 at 19:44
  • Are all the columns in the same order in each of the CSV files? If not then you might want to use smartbind from the gtools package. rather than bind_rows. As Mike said we need to see what the data looks like. Please read this article on how to create a reproduceable example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – robbieNukes Feb 01 '22 at 19:49
  • 1
    Likely duplicate: [Override column types when importing data using readr::read_csv() when there are many columns](https://stackoverflow.com/questions/31568409/override-column-types-when-importing-data-using-readrread-csv-when-there-are) or [Bind rows of different data types](https://stackoverflow.com/questions/39377370/bind-rows-of-different-data-types) – Ian Campbell Feb 01 '22 at 20:15
  • @Mike When the data was originally created, the column with NA was 'numeric' structure. But when I use data.frame(read.csv()) to examine individual files later, it's 'logi' structure. I would be happy to provide two example files on GoogleDrive or something, but I can't seem to find a way to reproduce the problem with syntax. Attempted to add something that maybe explains better in the way of syntax. – j400nb Feb 01 '22 at 20:29
  • In the examples you provided you are using base r to combine variables that are different data types which will allow you to combine them. bind_rows will not allow to you to combine rows of different data types and you have to covert them first. Ian's comment will help – Mike Feb 02 '22 at 14:58

0 Answers0