[R] create a factor variable from two numeric variables when order is irrelevant
Daniel Malter
daniel at umd.edu
Tue Jun 28 21:59:26 CEST 2011
Hi all,
I have two numeric variables that form combinations in a matched sample.
Let's say I have five levels of x and y. What I am seeking to create is a
factor variable that ignores the order of x and y, i.e., the factor should
indicate x=1, y=5, as the same factor as x=5, y=1. Obviously, this becomes
increasingly cumbersome to do by hand as the number of levels increases.
f<-1:5
x<-sample(f,100,replace=T)
y<-sample(f,100,replace=T)
d<-matrix(cbind(x,y),ncol=2)
#A working solution is to remove the order, multiply one column by a scaling
constant, add the second column, and create the factor for this numeric
value. However, I was wondering whether there is less awkward, more direct
way to do this.
i<-apply(t(apply(d,1,function(x) sort(x))),1,function(y) 10*y[1]+y[2])
i<-factor(i)
i
Thanks for your help,
Daniel
--
View this message in context: http://r.789695.n4.nabble.com/create-a-factor-variable-from-two-numeric-variables-when-order-is-irrelevant-tp3631318p3631318.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list