On Mon, Oct 20, 2008 at 11:14 AM, jim holtman <[EMAIL PROTECTED]> 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
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 212
5 0 2 1 3
6 0 1 2 2
or if x, y and z are factors :
> dbis <- as.data.frame(apply(d, 2, as.factor)
> dbis$x:dbis$y:dbis$z
[1] 1:1:1 0:3:2 0:2:1 1:3:2 0:2:1 0:1:2 1:2:1 0:3:2 0:2:1 1:3:2
12 Levels: 0:1:1 0:1:2 0:2:1 0:2:2 0:3:1 0:3:2 1:1:1 1:1:2 1:2:1 ... 1:3:2
and for your results :
> as.numeric(dbis$x:dbis$y:dbis$
On Mon, Oct 20, 2008 at 4:10 PM, <[EMAIL PROTECTED]> wrote:
>
> I'm trying to create a new column in my data.frame where subjects are
> categorized depending on values on four other columns. In any other case I
> would just nest a few ifelse statements, however, in this case i have
> 4*6*2*3=14
try this:
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 <- data.frame(x, y, z)
ind <- do.call("paste", c(expand.grid(1:2, 1:3, 0:1)[3:1], sep = "\r"))
trg <- do.call("paste", c(d, sep = "\r"))
d$myvar <- match(trg, ind)
I hope it helps.
Best,
Dimitris
I'm trying to create a new column in my data.frame where subjects are
categorized depending on values on four other columns. In any other case I
would just nest a few ifelse statements, however, in this case i have
4*6*2*3=144 combinations and i get weird 'context overflow' errors. So I wonder
6 matches
Mail list logo