Can't Combine X <character> and X <double>
Asked Answered
D

2

8

I am trying to merge various .csv files into one dataframe using the following: df<- list.files(path = "C:/Users...", pattern = "*.csv", full.names = TRUE) %>% lapply(read_csv) %>% bind_rows clean

However, I get an error saying that I can't combine X character variable and X double variable. Is there a way I can transform one of them to a character or double variable?

Since each csv file is slightly different, from my beginners standpoint, believe that lapply would be the best in this case, unless there is an easier way to get around this.

Thank you everyone for your time and attention!

Dionnadionne answered 30/6, 2021 at 1:30 Comment(3)
You're using read_csv, you can use col_types= to set the classes. See readr.tidyverse.org/articles/readr.htmlGaynell
If each CSV file is different, then I don't see how doing a blind bind_rows is a safe or sane approach. It would help to see a few rows from a few files to see what you mean by "slightly different".Gaynell
Sorry for not clarifying but by slightly different, I mean that some CSV files have 2-3 more columns than the rest, and that the ones that they do share are identical.Dionnadionne
O
7

You can change the X variable to character in all the files. You can also use map_df to combine all the files in one dataframe.

library(tidyverse)

result <- list.files(path = "C:/Users...", pattern = "*.csv", full.names = TRUE) %>% 
  map_df(~read_csv(.x) %>% mutate(X = as.character(X)))

If there are more columns with type mismatch issue you can change all the columns to character, combine the data and use type_convert to change their class.

result <- list.files(path = "C:/Users...", pattern = "*.csv", full.names = TRUE) %>% 
  map_df(~read_csv(.x) %>% mutate(across(.fns = as.character))) %>%
  type_convert()
Oospore answered 30/6, 2021 at 3:9 Comment(3)
Thank you for your reply. I tried it but it keeps telling me the same issue. Could it be that because some CSV's have more columns than others, that it is not letting it work?Dionnadionne
Your first suggestion works, but then it tells me that other variables are inconsistent and when I tried doing it to all columns as your second suggestion mentions, that is when it does not work.Dionnadionne
It means that you have other columns in your data apart from X which has the same problem that is why when we change all the columns to characters it works.Oospore
A
4

if all file has same number of columns, then try plyrr::rbind.fill instead of dplyr::bind_rows.

 list.files(path = "C:/Users...", pattern = "*.csv", full.names = TRUE) %>% lapply(read_csv)  %>% plyrr::rbind.fill
Avaricious answered 30/6, 2021 at 4:30 Comment(1)
This works, with the small edit that it is plyr not plyrrCoaler

© 2022 - 2025 — McMap. All rights reserved.