Reputation: 53
I am new to R
this is the code I am using
dataframe = data.frame(listData)
FBR1 = bind_rows(listData)
and this is the error message that I get
"Can not automatically convert from factor to integer in column "V6"".
I want to keep the V6 variable as a factor. Or if this cannot be possible I would like to know an efficient method to perform this conversion. This conversion indeed should not be applied to V6 alone as in the data frame there are similar columns (i.e. V6.1, V6.2 until V6.250!) that contain the same kind of information (word text: "left", "right").
I also wonder how with bind_rows function it was possible to convert information about the gender codified as "male", "female" in variable "V1" "V1.1"..."V1.250"
Upvotes: 0
Views: 4242
Reputation: 1544
I've developed a minimal example, and a solution. I assume you want to keep every column as a factor here. So, we first strip the factors and convert everything to character. Then bind_rows()
can be used without problem, and all columns can be converted to factors again, keeping all distinct values as levels.
If only specific columns need to be treated in this way, use the vars
argument of mutate_each()
.
library(dplyr)
# A minimal example
df1 <- data.frame(a=factor(letters), b=1:26)
df2 <- data.frame(a=1:10, b=factor(letters[1:10]))
dfl <- list(df1,df2)
# This would generate your error
# df <- bind_rows(dfl)
# This would solve it while keeping the factors
df <- dfl %>%
lapply(function(x) mutate_each(x, funs('as.character'))) %>%
bind_rows() %>%
mutate_each(funs('as.factor'))
Upvotes: 4