Question

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?

Was it helpful?

Solution

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

OTHER TIPS

Use transform and paste0

> x <- transform(x, Factor=paste0(var1, var))
  var1 var Factor
1    0   0     00
2    0   1     01
3    1   0     10
4    1   1     11

> sapply(x, class) # checking class for each column
     var1       var    Factor 
"numeric" "numeric"  "factor"  

Another alternative is using within

x <- within(x, Factor <- factor(paste0(var1, var)))

Just create a new column - use factor and paste0:

> x$f = factor(paste0(x$var1,x$var))
> x
  var1 var  f
1    0   0 00
2    0   1 01
3    1   0 10
4    1   1 11
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top