In R, since nominal, ordinal, interval, and ratio variable are treated differently in statistical modeling, we have to convert a nominal variable from a character or int into a factor.
You need to have completed the previous recipe, that is, viewing the airquality dataset using the str
and head
commands, to get an idea about what it contains.
Perform the following steps to convert the types of character variables:
- Assign the airquality dataset to mydata:
> mydata <- airquality
- To transform the variable from the
int
numeric type to thefactor
categorical type, you can castfactor
:
> mydata$Month = factor(mydata$Month)
- Print out the variable with the
str
function and again, you can see thatMonth
is now transformed into the factor as follows:
> str(mydata) Output 'data.frame': 153 obs. of 6 variables:$ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ... $ Solar.R: int 190 118 149 313 NA NA 299...