Error message received on command: combined_rows <- bind_rows

Hello - I've pre-cleaned and uploaded 12 Excel spreadsheets named df1 -df12 and have tried some different commands to combine the 12 spreadsheets with identical column headers so I can run ggplot and produce visualizations. Getting error messages when I run combined_rows <- bind_rows and failing that, merged_rows <- bind_rows. I clicked on the error link but don't understand what its meaning is. Is there a syntax I can use to compare and verify that all 12 spreadsheets' column headers are identical? I've looked and they look perfect but maybe I've missed something. Once they've been verified, what is the best command syntax to run? To simplify my syntax, should I just focus on the 2 or 3 columns I'm interested in visualizations? Here is the domain: Posit Cloud

and here is the coding and error messages in case the domain doesn't work for you (thanks in advance):

combined_df <- bind_rows(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12)
Error in bind_rows():
! Can't combine ..1$...8 <datetime> and ..12$...8 .
Run rlang::last_trace() to see where the error occurred.

rlang::last_trace()
<error/vctrs_error_ptype2>
Error in bind_rows():
! Can't combine ..1$...8 <datetime> and ..12$...8 .

and here is the coding and error message for the other command I ran:

merged_df <- bind_rows(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12)
Error in bind_rows():
! Can't combine ..1$...8 <datetime> and ..12$...8 .
Run rlang::last_trace() to see where the error occurred.

rlang::last_trace()
<error/vctrs_error_ptype2>
Error in bind_rows():
! Can't combine ..1$...8 <datetime> and ..12$...8 .


Backtrace:

  1. └─dplyr::bind_rows(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12)
    Run rlang::last_trace(drop = FALSE) to see 10 hidden frames.

At a quick guess your data sets are reading in the data in different formats.
Here is an example of what I mean.

It looks to me that you can use the compare_df_cols() function in the {janitor} package to check this.

library(janitor)
library(dplyr)

## Create two data.frames, dat1 & dat2
dat1  <- dat2 <- data.frame(ls = sample(LETTERS[1:16], 16, replace = FALSE), 
                            nn = sample(1:10, 16, replace = TRUE))

bind_rows(dat1, dat2)

compare_df_cols(dat1, dat2)


## change nn to a character variable.

dat2$nn  <- as.character(dat2$nn)

bind_rows(dat1, dat2)
compare_df_cols(dat1, dat2)

Thanks for your quick look. As a newbie to RStudio, I was unclear on whether I needed to use that syntax verbatim or if some things needed my input, or what character variable means. Also, compare_df_cols(dat1, dat2) wasn't sure if I should replace dat1 with df1 since that is what I was using. Anyway, I tried various ways including verbatim but got error messages. So I put my problem to chatgpt and it spit the following code, which merely changes the bind_rows I used to rbind: combined_df <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12)

That did the trick or so I thought until I tried to run ggplot, and got error messages which I ascertained after some time was because after all the work cleaning up 12 spreadsheets in Excel, then R, then back in Excel for a final clean before exporting back to RStudio - that I was saving in the wrong format (Excel Workbook). The column names and data types were distorted once they got back to RStudio because, apparently, I didn't save them in csv after the final Excel cleaning. Live and learn.

isn't the problem with bind_rows, it's that

are mixed types. Check with

# make first variable character, for demo
mtcars[,1] <- as.character(mtcars[,1])
sapply(mtcars,typeof)
#>         mpg         cyl        disp          hp        drat          wt 
#> "character"    "double"    "double"    "double"    "double"    "double" 
#>        qsec          vs          am        gear        carb 
#>    "double"    "double"    "double"    "double"    "double"

Created on 2023-08-12 with reprex v2.0.2

My apologies. No you would want to use your data.frame names. I use dat1 & dat2 as generic names and I forgot to change them back to your names.

I think you can probably do:

compare_df_cols(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12)

R can be very fussy . I know when I first started using it, I spend many frustrated hours importing data. It gets better fairly quickly.

I didn't save them in csv after the final Excel cleaning

Been there, done that.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.