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:
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);
Grab the columns of each
df
from the error message;Adapt the class attribute of df1 to df2 (or vice versa);
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)