0

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"

1
  • 1
    You should create a reproducible example and include that in the question. We have no way of knowing what's really in your data. Include sample input and desired output. Commented Dec 15, 2016 at 17:01

1 Answer 1

4

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'))
Sign up to request clarification or add additional context in comments.

1 Comment

also see data.table::rbindlist(dfl)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.