Laura Ataraxia
Laura Ataraxia

Reputation: 11

How to create a new variable merging 2 dichotomous Variables?

Im writing about how parents tweet about their children. My Dataset consists of a metric variable (score in emotions after sentiment analysis) and 2 dichtomous varibales (sex of the child: "son", daughter, sex of the parent: "female", "male").

Now i want to create a new categorial variable called "combination". So i get mother/daughter, mother/son, father/daughter, father/son for creating a boxplot mit means and sd (consisting of 4 groups). Does anybody have a idea how this can be done?

Upvotes: 0

Views: 130

Answers (4)

EcologyTom
EcologyTom

Reputation: 2500

Other answers here are great. This is the lazy approach if you want an intermediate step, which will create your combination variable for use in other analyses, then try

data$combination <- paste(data$parent, data$child, sep = ".")

Upvotes: 0

lebatsnok
lebatsnok

Reputation: 6459

interaction is specifically meant for cases like this. It does a little bit more than paste: the result is a factor with all possible combinations of x and y as levels -- for example, if there is no "father/daughter" combination in the data, this will still be a level of the resulting factor by default (this can be turned out by setting drop = TRUE):

with(df, interaction(parent, child))

Upvotes: 2

P1storius
P1storius

Reputation: 947

A quick solution would be to paste the sex labels together

set.seed(1234)
d <- data.frame(parent=c(rep('male',6),rep('female',4)), 
       child=c(rep('son',3),rep('daughter',6),rep('son',1)),
       emotion=sample(1:4,10,replace=T))

d$combination <- paste0(d$parent,'/',d$child)

Upvotes: 0

DanY
DanY

Reputation: 6073

# example dataset
df <- data.frame(
    score  = runif(100, 1, 10),
    child  = sample(c("son", "daughter"), 100, replace=TRUE),
    parent = sample(c("mother", "father"), 100, replace=TRUE)
)

# boxplot of 4 child*parent combinations
boxplot(score ~ child:parent, data=df)

enter image description here

Upvotes: 1

Related Questions