Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new variable by combining 2 Categorical variables in R

mydata$gender <- c("M", "F", "M")
mydata$country <- c("USA", "USA", "USA")

Create a new variable by combining gender and country. Variable will state "M USA".

So I can't figure out how to combine these 2 categorical data to produce what I want.

I assume that recoding them would take to long.

Is there a way to do this without using the interaction function?

Thanks in advance.

like image 315
EpikSnow Avatar asked Sep 18 '25 00:09

EpikSnow


1 Answers

Something like this...?

> transform(mydata, newvar=paste(gender, country))
  gender country newvar
1      M     USA  M USA
2      F     USA  F USA
3      M     USA  M USA
like image 174
Jilber Urbina Avatar answered Sep 19 '25 18:09

Jilber Urbina