[R] Combining all possible values of variables into a new...

hadley wickham h.wickham at gmail.com
Mon Oct 20 18:21:02 CEST 2008


On Mon, Oct 20, 2008 at 11:14 AM, jim holtman <jholtman at gmail.com> wrote:
> yet another way (as it always is in R):
>
>> x<-c(1,0,0,1,0,0,1,0,0,1)
>> y<-c(1,3,2,3,2,1,2,3,2,3)
>> z<-c(1,2,1,2,1,2,1,2,1,2)
>> d<-as.data.frame(cbind(x,y,z))
>> d$myvar <- x*6 + (y-1)*2 + z
>>
>> d
>   x y z myvar
> 1  1 1 1     7
> 2  0 3 2     6
> 3  0 2 1     3
> 4  1 3 2    12
> 5  0 2 1     3
> 6  0 1 2     2
> 7  1 2 1     9
> 8  0 3 2     6
> 9  0 2 1     3
> 10 1 3 2    12

That's equivalent to as.numeric(interaction(x, y, z)) but in my
experience is much much faster (which of course only matters for huge
datasets).

Hadley


-- 
http://had.co.nz/



More information about the R-help mailing list