# Re: [R] random value changes in a vector

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Tue, 18 Nov 2008 14:19:25 -0500

It's generally better to have a descriptive subject as you have done here but not in your prior psoting.

You might think about how you can use this sort of result:

vec <- c(1, -1, -1, 1, 1, -1)
sample(c(-1,1),length(vec),replace=T)

Perhaps:
vec <- vec*sample(c(-1,1),length(vec),replace=T)

?sample # should explain how probabilities other than 0.5 can be specified

On Nov 18, 2008, at 12:11 PM, Salas, Andria Kay wrote:

> To clear up a question regarding my earlier posting regarding random
> changes in a vector:
>
> Say you have a vector (1, -1, -1, 1, 1, -1). I want each value in
> the vector to have a probability that it will change signs when this
> vector is regenerated. For example, probability = 50%. When the
> vector is regenerated, the first value in the vector (1) has a 50%
> chance of switching to -1. If I regenerated this vector 10 times, 5
> of the times it would switch to -1.

Only "on average" would that be true. It might not switch at all if the process is truly random.

```--
David Winsemius

> Similarly, I need each value in the vector to have this same

> probability of switching signs when the vector is regenerated, and
> each value's chances of doing so is independent of the other
> values.  So the second value (-1) also has a 50% chance of switching
> to 1, and whether or not it does so is independent of if the first
> value changes from 1 to -1 (also a 50% probability).
>
> I hope this clears up the confusion, and I would appreciate any help
> anyone can provide!
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help