0

I have a question and in the last four hours, I could not the solution, so I was wondering if anyone of you could help me out. To see my problem, let us start with an example:

tmp <- matrix(1:24, ncol=2)
fac <- rep(1:5, length.out=12)

Based on the factor levels fac, I want to apply a function to all elements of tmp that share the same factor levels. I ended up using by, but if this is not appropriate, please don't hesitate to point it out.

new <- by(tmp, fac, function(x) x <- x*sign(rbinom(1,1,.5)-0.5) )

This will multiply each group by either 1 or -1.

Now the problem: I wish to end up with the same matrix as before, i.e. with tmp in my example (just that some groups of observations have been multiplied with -1). I have tried the following:

new.tmp <- matrix(unlist(new), ncol=ncol(tmp))

But as it turns out, the order of the rows is mixed up. This is understandable since the by command coerces my matrix into a dataframe, and takes subsets of the data frame based on my factor. Is there a way to put everything back together so that the initial row order is preserved?

Note that unlike in my example matrix, my actual dataset does not have ordered values in the columns.

I would really appreciate any help!

4

1 回答 1

3

我认为这应该有效:

tmp     = matrix(1:24, ncol=2)
fac     = rep(1:5, length.out=12)
new     = tapply(fac, fac, function(x) sign(rbinom(1,1,.5)-0.5) )

new.tmp = tmp*as.numeric(new[fac])
于 2013-08-03T14:08:26.823 回答