I have 2 lists of dataframes (df1 and df2). The lists and dataframes are identical in their classes, and 4 columns, but are not identical in the number of rows or data they contain:
df1.1=
Col1 Col2 Col3 Col4
chr chr num num
df2.1 =
Col1 Col2 Col3 Col4
chr chr num num
etc for 100 dataframes per list.
They are stored in a master list of dataframes (df1) that I use lapply functions on.
I want to reduce all the dataframes in each list to one dataframe. To do this I used:
reducedf1<-df1 %>% reduce (full_join);
reducedf2<-df2 %>% reduce(full_join);
For df1 it worked. For df2 it did not. The error given was:
Error in full_join_impl(x,y, by_x, by_y, aux_x, aux_y, na_matches) : Can't join on 'Col2' x 'Col2' because of incompatible types (integer/character).
To fix this I tried to check if there were dataframes inside the list that did not have the same class and correct them:
testingfunction<-function(x){
col_to_change=x[,2];
class(col_to_change)<-"character";
x}
mutatedf2<-lapply(df2, testingfunction)
I checked- still has class character in every dataframe, but does not join- same error.
If my dataframes all have the same classes and they were created in the same way and put in the same list- why in one dataframe would it work and one it would not? What would be a way to solve this error so I can merge the dataframes in the list to one large dataframe using reduce and full_join?
col_to_change=[x,2];is doing why not just haveclass(x[[2]])<-"character";xin your function. Better yet why don't you runsapply(df2, function(x) class(x[[2]]))to find out which dataframe have a different class so you can actually check what is causing the discrepency - Sarah