I have a data frame:
> x = data.frame(var1 = c(0,0,1,1), var = c(0,1,0,1))
I would like to add another column to that data frame that is factor, set based on the values of var1 and var2.
factor "00" if both are 0
factor "10" if var1 = 1 and var2 = 0
factor "01" if var1 = 0 and var2 = 1
factor "11" if both are 1
In reality I have about 10 variables and need the factors to generate cross-tables to check how other variables are influence by factors.
I could write if statements to get this done, however I think there must be a smarter way of doing that. Any suggestions?
You are looking for interaction
:
transform(x, Factor=interaction(var1, var,sep=''))
var1 var Factor
1 0 0 00
2 0 1 01
3 1 0 10
4 1 1 11