Dear Murali,
Here is one way:


set.seed(123)
a <- matrix(abs(rnorm(100)), 10)
b <- rnorm(10)
apply(a,2,function(x) ifelse(b>quantile(b,probs=0.75),-x,x))


HTH,

Jorge





On Wed, Nov 5, 2008 at 11:30 AM, <murali.menon@fortisinvestments.com> wrote:

> Folks,
>
> I have a matrix:
>
> set.seed(123)
> a <- matrix(rnorm(100), 10)
>
> And a vector:
>
> b <- rnorm(10)
>
> Now, I want to switch the signs of those rows of a corresponding to
> indices in b whose values exceed the 75 %-ile of b
>
> which(b > quantile(b)[4])
> [1] 2 6 10
>
> so I want, in effect:
>
> a[2, ] <-  -a[2, ]
> a[6, ] <- -a[6, ]
> a[10, ] <- -a[10, ]
>
> I thought I could do
>
> a[which(b > quantile(b)[4]), ] <- -a
>
> but that's clearly wrong.
>
> I came up with an sapply():
>
> t(sapply(1 : NROW(a), function(n) ifelse(b > quantile(b)[4], -a[n, ], a[n,
> ])))
>
> Ugh.
>
> What's a good way to achieve this?
>
> Thanks,
>
> Murali
>
>
> ---------------------------------------------------------------------------
> This e-mail is confidential and may be privileged. If you have received it
> by mistake, please notify the sender by return e-mail and delete it from
> your system. You should not disclose, copy or use it for any purpose. The
> information in this e-mail is not contractual. Fortis Investments provides
> no guarantee as to the correctness of this information and accepts no
> responsibility for any action taken on the basis of it. Fortis Investments
> is the trade name for all entities within the Fortis Investment Management
> group.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

