'Bind rows with different data types
I have some dataframes with more than 3000 columns in each, and I want to bind them together.
When I use
library(dplyr)
bind_rows(dataframe1, dataframe2, dataframe3, dataframe4)
I get a lot of warnings:
In bind_rows_(x, .id) : Unequal factor levels: coercing to character
...
I guess it's because a column has data of type factor in one dataframe and data of type character in another dataframe. But how can I solve this problem?
I know I can use
sapply(dataframe1, class)
to get the classes of a dataframe, but as there are many columns, it is impossible to go through them all in all 4 dataframes.
This seems to be a problem about the data, but what does it mean that something has type factor? Is it a number?
Solution 1:[1]
Perhaps start with ?factor about what factors are.
To avoid the warnings, you either use supressWarnings, or you will need to convert to character first. For example (untested):
library(tidyverse)
l <- list(dataframe1, dataframe2, dataframe3, dataframe4)
map_dfr(l, ~mutate(., across(where(is.factor), as.character))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
