How to recode ordinal variable?

Varuna99

I am using survey data from the World Values Survey, I used the code below to change my variable from a numeric to an ordered variable

renameddata$Education= ordered(renameddata$Education, levels =c(-2,-1,840001,840002,840003,
                                         840004,840005,840006,840007,
                                         840008,840009),
        labels = c("NA","NA","LessHighSchool","SomeHighSchool",
                   "GED","SomeCollege","Associates","Bachelors",
                   "Masters","Professional","Doctorate"))

However, now I want to recode the education variable so that LessHighSchool and SomeHighSchool become one e.g "NO GED", and so that SomeCollege, Associates and Bachelors become "Undergraduate" etc.

deschen

Alternatively, if you want to recode the created factor variable, you can use fct_collapse from the forcats package:

Input:

renameddata <- data.frame(Education = c(-2, -1, 840001, 840002, 840003, 840004, 840005, 840006, 840007, 840008, 840009))

renameddata$Education = ordered(renameddata$Education,
                                levels = c(-2, -1, 840001, 840002, 840003, 840004, 840005, 840006, 840007, 840008, 840009),
                                labels = c("NA", "NA", "LessHighSchool", "SomeHighSchool", "GED", "SomeCollege", "Associates", "Bachelors", "Masters", "Professional", "Doctorate"))

Recoding:

library(forcats)
renameddata$Education <- fct_collapse(renameddata$Education,
                                      "NO GED" = c("LessHighSchool", "SomeHighSchool"),
                                      "Undergraduate" = c("SomeCollege", "Associates", "Bachelors"))

gives:

       Education
1             NA
2             NA
3         NO GED
4         NO GED
5            GED
6  Undergraduate
7  Undergraduate
8  Undergraduate
9        Masters
10  Professional
11     Doctorate

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to recode numerical variable into categorical?

How to recode this string variable into a new variable?

How to recode a variable programmatically using quasiquotation?

How do I recode character variable?

How to recode time variable into number in R (dataframe)

How transform (calculate) an ordinal variable to a dichotomous in R?

How can I recode this variable in R (reverse coding)

How do I recode a character variable containing certain letters?

How to recode variable to a categorical type based on list of row numbers

Mutate changed, how to recode into new variable with excluded values defined as NA

Recode A String Variable

Ordinal variable to become dichotomous

Coverting a numeric variable into a ordinal variable

Recode catagorical variable as integers in Pandas

Recode with a variable number of cases in R

Most efficient way to recode variable?

R: How to recode values of a variable to NA for cases where another variable has a value of NA

How can I recode an event-based variable into a value-based variable in R?

dplyr - how to recode to NA?

How to recode strings in Stata?

How to count a numeric variable then create a new ordinal count variable by unique identifier

How to modify the default variable type defined by "all_categorical()" in "gtsummary"? when mean of ordinal variable were wanted?

Recode variable values into strings based on different variable

How to recode continuous age in age categories and create a new variable at the same time

How to recode a new date variable and select the lowest date out of four date columns in R

Using an IF statement to create an ordinal variable

Variable band sizes with ordinal scale

OrdinalEncoder: pass levels to ordinal variable

How to Validate Ordinal Dates?