0

I have posted a question pertaining to the code:

df_merged <- rbindlist(list(df1, df2), fill=TRUE, use.names=TRUE)

Which resulted for me resulted in the following error message:

Error in rbindlist(list(df1, df2), fill = TRUE, use.names = TRUE) : 
  Class attribute on column 2 of item 2 does not match with column 2 of item 1.

It got me wondering. Is is possible to:

  1. Store this error message in a variable. I have tried to see how I could apply the following link, but I do not completely understand the solutions provided (LINK);

  2. Grab the columns of each df from the error message;

  3. Adapt the class attribute of df1 to df2 (or vice versa);

  4. Try the merge again

Something like:

library(stringr)
df_merged <- rbindlist(list(df1, df2), fill=TRUE, use.names=TRUE)
errormessage1 <- error()
df1col <- word(errormessage1 , start = -4, end = -4)
df2col <- word(errormessage1 , start = -13, end = -13)
class(df2[,"df2col"]) <- class(df1[,"df1col"])
df_merged <- rbindlist(list(df1, df2), fill=TRUE, use.names=TRUE)
Tom
  • 2,173
  • 1
  • 17
  • 44
  • help("tryCatch") – Roland Apr 18 '19 at 11:06
  • I don't think the idea is a good one. To be robust you always want to control your variable classes. The classes up to this point should be the same. The fault lies somewhere upstream of this rbindlist. My suggestion would be create a lookup table with the column name and class, and then apply these to your datasets prior to using the rbindlist. – Jonny Phelps Apr 18 '19 at 11:08
  • @JonnyPhelps I agree. And I did make a lookup table. I also checked that the approach is safe for the datasets concerned. Lastly I simply do not have the time to manually go through 27 dataframes with possibly multiple class issues.. But I understand your comment.. – Tom Apr 18 '19 at 11:15

0 Answers0