[R] Index and dummy

Marujo A. A.Marujo at soton.ac.uk
Sun Jul 19 23:32:20 CEST 2009


Dear R-helpers

I have 2 variables
x1=rgamma(6000, 2, 1) and x2=rgamma(6000, 3,2). I have to sort (descending) each one and split it into groups. After this each two groups must be merged into one until all population becomes one group. A dummy vector must be created for each group (8, 4, 2, 1) being equal to 1 if the individual (i) belongs to the group and equal to 0, otherwise.

What I have done was:
id=(6000)
x1sort=sort(x1, decreasing=TRUE)
x1g8_1=x1sort[1:750]
x1g8_2=x1sort[751:1500]
x1g8_3=x1sort[1501:2250]
x1g8_4=x1sort[2251:3000]
x1g8_5=x1sort[3001:3750]
x1g8_6=x1sort[3751:4500]
x1g8_7=x1sort[4501:5250]
x1g8_8=x1sort[5251:6000]

x1g4_1=c(x1g8_1, x1g8_2)
x1g4_2=c(x1g8_3, x1g8_4)
x1g4_3=c(x1g8_5, x1g8_6)
x1g4_4=c(x1g8_7, x1g8_8)

x1g2_1=c(x1g4_1, x1g4_2)
x1g2_2=c(x1g4_3, x1g4_4)

x1ng=c(x1g2_1, x1g2_2)

After this I did the dummy vector (the example is for group4)

dum= replace(matrix(0, 4, 1), cbind(4, 1), 0)                           # matrix of zeros
dummy=lapply(1:4, function(i) replace(dum, cbind(i), 1))        # 4 dummy vectors
s=split(dummy, 1:4)
ss=rename.vars(s, c("1", "2", "3", "4"), c("dx14_1", "dx14_2", "dx14_3", "dx14_4"))

The problem is when I split into groups each group only identifies 750 individuals(in the case of x1g8 for instance) only assumes i=1, ..., 750 and I need to keep i=1, ...., 6000. Also my option to dummy vectors don't seem to work because I get 4 vectors with the number one (1) in each different variable and not only one.

So, I need some help on how should I make to keep i=1, ..., 6000 and how to create a dummy vector that assumes only the value one (1) when some I belongs to some group.

Thank you so much.
Ana




More information about the R-help mailing list