简体   繁体   中英

Is there an R function for converting a Categorical Variable (in Character) to a Quantitative Variable?

I have a categorical variable (in character structure) that is dummy coded in the following manner xx-xxxx. The first 2 digits of the dummy code are significant for categorizing the responses. I would like to be able to bin the responses according to these first 2 digits. For example, there are 28 responses dummy coded as 11-xxxx. I would like to combine all 28 of these responses into one response. I would, therefore, like to be able to convert the dummy coded categorical variable to a quantitative variable so I can more efficiently bin the responses according to these first 2 digits. Is there an R function for making this conversion?

Image of the Frequency Distribution of the first few responses for the variable

I am a beginner coder and this is my first time posting to stack overflow. Thank you for your help!

dput(data$H4LM18) Sample

I was able to receive help from a Help Desk and we successfully binned the variable according to the first two digits of the dummy code.

Here is code used for the dataset data and the variable H4LM18:

data$jobcategory<-data$H4LM18

data$jobbracket <- unlist(lapply((strsplit(data$jobcategory, "-")),function(x){x[1]}))#[c(T, F)]

By splitting the dummy code of the responses at the dash ("-") we were able to categorize the responses according to the first two digits of the dummy code alone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM