Dear Friends, I'm trying to generate a sequence of 100 observations with either a 1 or -1. In other words the sequence should look something like this. y = 1 1 -1 1 -1 -1 -1 1 1 ...... Can somebody please give me some direction on how I can do this in R. Thanks Anup ____________________________________________________________________________________ Don't get soaked. Take a quick peak at the forecast
Hi Anup, (runif(100)<.5)*1 #would give you 0's and 1's. sample(rep(c(-1,1),50),100) #A bit slower I think, gives you -1's and 1's Best, Matt On 4/12/07, Anup Nandialath <anup_nandialath at yahoo.com> wrote:> Dear Friends, > > I'm trying to generate a sequence of 100 observations > with either a 1 or -1. In other words the sequence > should look something like this. > > y = 1 1 -1 1 -1 -1 -1 1 1 ...... > > Can somebody please give me some direction on how I > can do this in R. > > Thanks > > Anup > > > > ____________________________________________________________________________________ > Don't get soaked. Take a quick peak at the forecast > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Matthew C Keller Postdoctoral Fellow Virginia Institute for Psychiatric and Behavioral Genetics
Anup Nandialath wrote:> Dear Friends, > > I'm trying to generate a sequence of 100 observations > with either a 1 or -1. In other words the sequence > should look something like this. > > y = 1 1 -1 1 -1 -1 -1 1 1 ...... > > Can somebody please give me some direction on how I > can do this in R. >sample(c(-1, 1), 100, replace=TRUE)
There are many ways to do this. The first that comes to my mind is sample(c(1,-1),100,TRUE). Notice that sample also has a prob argument that may be useful for you. Francisco Anup Nandialath wrote:> Dear Friends, > > I'm trying to generate a sequence of 100 observations > with either a 1 or -1. In other words the sequence > should look something like this. > > y = 1 1 -1 1 -1 -1 -1 1 1 ...... > > Can somebody please give me some direction on how I > can do this in R. > > Thanks > > Anup > > > > ____________________________________________________________________________________ > Don't get soaked. Take a quick peak at the forecast > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >