September 2018
Beginner
206 pages
4h 27m
English
Herein, we will create factor variables in a dataset both one at a time and by using a method that converts multiple variables at once. In order to do so, the following steps have to be executed:
Load the datasets library:
library(datasets)
Load the midwest dataset and examine it with str():
data(midwest) str(midwest)
Convert the state variable to a factor by using as.factor():
midwest$state <- as.factor(midwest$state)
Load the band_instruments dataset and examine it with str():
data(band_instruments)str(band_instruments)
Transform both variables in band_instruments to factor variables using apply():
band_instruments <- data.frame(apply(band_instruments, 2, as.factor))