I am working on a data set that contains a variable called "JobSatisfaction" that is shown as int [1,2,3,4]. In order to proceed with my data analysis, I need to convert this variable into a ordinal variable.
JobSatisfaction is supposed to have four levels: low, medium, high, very high
After that I will need to do a linear regression.
You can just use ordered(). Here is an example
df <- data.frame(code = rep(letters[1:4], 5),
JobSatisfaction = sample(rep(1:4,5))) # data frame df
df$JobSatisfaction <- ordered(df$JobSatisfaction, levels = 1:4,
labels = c("low", "medium", "high", "veryhigh")) # conversion
You are asking for a solution that convert a integer numeric variable into an ordered categorical variable.
I suggest you to use the package dplyr because for this task it is perfect.
dplyr::mutate(my_df,
JobSatisfaction =
dplyr::recode_factor(JobSatisfaction,
`1`="low", `2`="medium",
`3`="high", `4` = "very high",
.ordered = TRUE)
recode is used for mapping the value of a variable to new values
the parameter .ordered = TRUE allows us to define that low < medium < high < very_high
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With