I need some help tidying my data. I\'m trying to convert some integers to factors (but not all integers to factors). I think I can do with selecting the variables in question
You can use mutate_at
instead. Here's an example using the iris
dataframe:
library(dplyr)
iris_factor <- iris %>%
mutate_at(vars(Sepal.Width,
Sepal.Length),
funs(factor))
As of dplyr 0.8.0, funs()
is deprecated. Use list()
instead, as in
library(dplyr)
iris_factor <- iris %>%
mutate_at(vars(Sepal.Width,
Sepal.Length),
list(factor))
And the proof:
> str(iris_factor)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: Factor w/ 35 levels "4.3","4.4","4.5",..: 9 7 5 4 8 12 4 8 2 7 ...
$ Sepal.Width : Factor w/ 23 levels "2","2.2","2.3",..: 15 10 12 11 16 19 14 14 9 11 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...