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

From: joris meys <jorismeys_at_gmail.com>
Date: Tue, 18 Nov 2008 23:23:08 +0100

Just for clarification : this is an extension, where you can take the probability of switching to any possible value. Take into account that the p-value in the function rbinom is 1-P for the switching of the sign.

On Tue, Nov 18, 2008 at 11:21 PM, joris meys <jorismeys_at_gmail.com> wrote:
> The function rbinom might be a solution.
>
> Try following simple program :
> vec <- c(-1,1,-1,1,1,-1,-1,1,1)
>
> inv <-rbinom(length(vec),1,0.5)
> inv <-ifelse(inv==0,-1,1)
>
> vec2 <- vec*inv #switches sign with p=0.5
>
> In this, inv is a random binomial vector, where the probability for
> being 1 is 0.5 in all positions. Changing the values of 0 in this
> vector to -1, gives you a vector you can multiply with the original
> one to change the sign with a probability of 0.5 for all positions
>
> Kind regards
> Joris
> On Tue, Nov 18, 2008 at 6:11 PM, Salas, Andria Kay <aks2515_at_uncw.edu> 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. 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