EpikSnow
EpikSnow

Reputation: 41

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.

Upvotes: 4

Views: 15060

Answers (2)

Michael Davidson
Michael Davidson

Reputation: 1411

paste(mydata$gender, mydata$country,sep=" ")

Upvotes: 3

Jilber Urbina
Jilber Urbina

Reputation: 61154

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

Upvotes: 7

Related Questions