mmyoung77
mmyoung77

Reputation: 1417

Transforming a Numeric variable into a Factor in dplyr

My results data are categorized by whether the experiment they came from had 8 test groups or 16 test groups:

set.seed(123)
results <- runif(10)
size <- c(16, 16, 16, 8, 16, 8, 8, 16, 16, 8)
df <- data.frame(results, size)

I want size to be a factor variable. In base R I would do this simply:

df$size <- as.factor(df$size)

My question is, how do you do the same in dplyr? I tried:

library(dplyr)
df %>%
  do(as_factor(size))

But I received the error no applicable method for 'as_factor' applied to an object of class "c('double', 'numeric')"

Upvotes: 1

Views: 849

Answers (2)

cardinal40
cardinal40

Reputation: 1263

You could try:

df %>% 
  mutate(size = as_factor(as.character(size)))

Once the variable has been converted to a character then the as_factor() call works.

Edit: The solution above is even better!

df %>% mutate(size = factor(size))

Upvotes: 2

akrun
akrun

Reputation: 887118

We can use mutate

library(dplyr)
library(magrittr)
df %<>%
   mutate(size = factor(size))

str(df)
#'data.frame':  10 obs. of  2 variables:
# $ results: num  0.288 0.788 0.409 0.883 0.94 ...    
# $ size   : Factor w/ 2 levels "8","16": 2 2 2 1 2 1 1 2 2 1

Upvotes: 4

Related Questions