How to one hot encode several categorical variables in R

0 votes
I'm working on a prediction problem and am using R to design a decision tree. I have multiple categorical variables that I'd like to one-hot encode consistently in both my training and testing sets. I was able to achieve that with my training data by:

X train - temperatures

tt - subset(temps, select = -output) tt - subset(temps, select = -output) tt - subset(temp

data.frame(model.matrix(. -1, tt), CLASS = temps$output) oh -

But I can't seem to figure out how to apply the same encoding to my testing set; what can I do?
Jun 19, 2022 in Data Analytics by Avinash
• 1,260 points
839 views

1 answer to this question.

0 votes

43
I advise employing the caret package's dummyVars function:

Customers (id=c(10, 20, 30, 40, 50), gender=c("male," "female," "male," "female," mood=c("happy," "sad," "happy," "sad," "happy," outcome=c(1, 1, 0, 0, 0))
customers' gender, mood, and result
1-10 joyful males 1 2 20 females who are dejected 1 3 30 females who are joyful 0 4 40 males who are dejected 0 5 50 females who are joyful 0

# obscurate the data
Data = customers, dmy - dummyVars(" ."
data.frame(predict(dmy, newdata = customers), trsf)
gender of trsf.
gender is feminine.
male emotion
good mood
tragic result
1 10 0 1 1 0 1 2 20 1 0 0 1 3 30 1 0 1 0 0 4 40 0 1 0 1 0 5 50 1 0 1 0 0 Example source

You conduct both using the same process.

Transform data into actionable insights with our Data Analyst Certification – Enroll today!

answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Analytics

0 votes
1 answer

How to create dummy variables based on a categorical variable of lists in R?

You can use mtabulate in the following way: library(qdapTools) cbind(data[1], ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
2,652 views
0 votes
1 answer

How to find out the sum/mean for multiple variables per group in R?

You can use the reshape2 package for ...READ MORE

answered Apr 12, 2018 in Data Analytics by DataKing99
• 8,250 points
3,858 views
0 votes
1 answer

How to set global variables in R

To set global variables, you can use ...READ MORE

answered Apr 24, 2018 in Data Analytics by kappa3010
• 2,090 points
1,303 views
0 votes
1 answer

How to remove all variables except functions in R?

One line that removes all objects except for functions: rm(list ...READ MORE

answered Apr 25, 2018 in Data Analytics by DeepCoder786
• 1,720 points
4,229 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,046 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
1,224 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,909 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,154 views
0 votes
1 answer

How to use Aggregate function in R

Where dat is the name of your ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
609 views
0 votes
1 answer

How to use the switch statement in R functions?

Switch definitely wasn't intended to operate this ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
607 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP