M Offenbacher
M Offenbacher

Reputation: 1

Converting columns of a dataframe to factor

I have a data frame with 100 columns and I want to convert them all into factor. Let's assume the data frame,

a <- as.integer(c(1,2,1,2,1,1))
b <- as.integer(c(1,2,3,3,3,1))
df <- data.frame(a,b)

I am trying this,

library(dplyr)
colwise(df, as.factor(df))

which give me an error like this,

> colwise(df, as.factor(df))
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

Any suggestion how to do this correct? Thanks in advance.

Upvotes: 0

Views: 87

Answers (1)

akrun
akrun

Reputation: 887118

With dplyr, we use factor within mutate_all for converting all columns to factor

library(dplyr)
df %>%
    mutate_all(factor)

Upvotes: 2

Related Questions