1

I have a data frame, where I would like to render some of the columns as factor (at the moment they are numeric).

For example:

dd = data_frame( x = c(0, 0, 0, 1, 1, 1), y = c(1,2,3,4,5,6))

I would like to make only the first column a factor:

lapply(dd[,1], as.factor)

But the result is a list (of a factor), and is not saved back to the original data frame.

Is there a way to achieve this?

Omry Atia
  • 2,411
  • 2
  • 14
  • 27

1 Answers1

2

We can use

library(dplyr)
dd <- dd %>%
         mutate(x = factor(x))

Or for multiple columns

nm1 <- names(dd)[1:2]
dd <- dd %>%
        mutate_at(vars(nm1), factor)

In the OP's code, the issue is that it is looping through the first column elements into a list. Instead, we need just

dd[,1] <- factor(dd[,1])

Or

dd[[1]] <- factor(dd[[1]])

NOTE: For a single column, we don't need any lapply

If we want to apply to multiple columns

dd[nm1] <- lapply(dd[nm1], factor)
akrun
  • 874,273
  • 37
  • 540
  • 662