I have two columns in the following data frame, and each column has levels according to a specific order:
head(x1)
soa congruency
1 200 9
2 102 2
3 68 1
4 68 9
5 34 9
6 68 9
head(levels(x1$soa))
[1] "34" "68" "102" "200"
head(levels(x1$congruency))
[1] "1" "2" "9
I want to be able to paste the two columns such that the levels of the new variable will be:
"34_1" "34_2" "34_9" "68_1" "68_2" "68_9" etc....
However if I do the following:
x2 <- paste(x1$soa, x1$congruency, sep = "_")
the levels I get are:
x2 <- factor(x2)
class(x2)
[1] "factor"
levels(x2)
[1] "102_1" "102_2" "102_9" "200_1" "200_2" "200_9" "34_1" "34_2" "34_9"
[10] "68_1" "68_2" "68_9"
I know I can change the order of the levels after I paste the columns. However I want to be able to order the columns such that after I paste them I won't need to change the order of the levels. Is there a way in which I can do that? For example I tried to order x1 using the order() function (and I did it correctly), and then paste the two columns but I still get the same order of levels which is not the order I want.
Any help will be greatly appreciated,
Ayala
You can try interaction
:
interaction(x1$soa, x1$congruency, sep= "_", lex.order = TRUE)
## [1] 200_9 102_2 68_1 68_9 34_9 68_9
## Levels: 34_1 34_2 34_9 68_1 68_2 68_9 102_1 102_2 102_9 200_1 200_2 200_9