[R] Sign of specific elements of a vector

Bert Gunter bgunter.4567 at gmail.com
Thu Jun 18 22:07:48 CEST 2015


Your **is** the "coolest" and most efficient way to do this. It's
vectorized -- apply() stuff is not.

Cheers,
Bert

Bert Gunter

"Data is not information. Information is not knowledge. And knowledge is
certainly not wisdom."
   -- Clifford Stoll

On Thu, Jun 18, 2015 at 12:40 PM, Frank S. <f_j_rod at hotmail.com> wrote:

> Hi everyone,
>
> I have an "x" vector and I would want to change the sign every 20
> elements. For this puspose,
> I wrote the following code:
>
> set.seed(1)
> x <- rnorm(100)
> x
> x[seq(20,100, by=20)] <- -x[seq(20,100, by=20)]
> x
>
> However, I'm afraid  it is a rudimentary form to get the desired result.
> II wonder wether there is a cool way to do so, that is, for example with
> apply or sign function.
>
> Thans in advanced for your help!
>
> Frank S.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]



More information about the R-help mailing list