# [R] Need some help

azzza azza.khogaliali at utoronto.ca
Tue Oct 16 03:06:21 CEST 2007

```
Thanks Jholtman.
However, the plot didnt come out the way I envisone dit to be. On the Y
axis, i should have sign changes in 1000 tosses, the range being from
negative to postitive, and a straight horizontal line across  y=0. The
X-axis should have the toss number, range 0-1000

I'm glad u mentioned rle(x)....works perfect!

jholtman wrote:
>
> I really depends on what you want to plot.  You can plot the
> cumulative count of the sign changes with:
>
> x <- sample(c(0,1), 1000, TRUE)
> plot(cumsum(x), type='l')
>
> There are other things you can do.  You can get a rough histogram of
> the length of the run by:
>
>> stem(rle(x)\$length)
>
>   The decimal point is at the |
>
>   1 |
> 00000000000000000000000000000000000000000000000000000000000000000000+201
>   1 |
>   2 |
> 00000000000000000000000000000000000000000000000000000000000000000000+60
>   2 |
>   3 | 00000000000000000000000000000000000000000000000000000
>   3 |
>   4 | 00000000000000000000000000000000000000
>   4 |
>   5 | 000000000
>   5 |
>   6 | 00000000
>   6 |
>   7 | 00000
>
> With R, almost anything is possible.
>
> On 10/15/07, azzza <azza.khogaliali at utoronto.ca> wrote:
>>
>> Umm, yes, what you had makes a lot of sense. How would I represent that
>> in a
>> plot of the number of sign changes in the to  Y axis, and the toss number
>> (from 0 to 1000) in the x-axis?
>>
>>
>>
>>
>> jholtman wrote:
>> >
>> > You might want to check out 'rle'.  This will give you the 'lengths'
>> > of runs of the same value and therefore when the value changes (sign
>> > change?) you can see how often:
>> >
>> >> x <- sample(c(-1,1), 1000, TRUE)
>> >> rle(x)
>> > Run Length Encoding
>> >   lengths: int [1:483] 2 2 1 4 3 1 1 1 1 2 ...
>> >   values : num [1:483] -1 1 -1 1 -1 1 -1 1 -1 1 ...
>> >
>> > Here was a sample of 1000, and there were 483 changes between the
>> > samples.  Is this what you are looking for?
>> >
>> > On 10/15/07, azzza <azza.khogaliali at utoronto.ca> wrote:
>> >>
>> >>
>> >> Quite helpful indeed. Greatly appreciated.
>> >> Another problem I had was trying to simulate an example from my book.
>> >> Simulating 1000 coin tosses, and finding the frequency of sign
>> changes.
>> >> So
>> >> how will we plot this using R? (frequency of sign changes in Y axis)
>> >>
>> >>
>> >>
>> >>
>> >> Daniel Nordlund wrote:
>> >> >
>> >> >> -----Original Message-----
>> >> >> From: r-help-bounces at r-project.org
>> >> [mailto:r-help-bounces at r-project.org]
>> >> >> On Behalf
>> >> >> Of azzza
>> >> >> Sent: Sunday, October 14, 2007 10:21 PM
>> >> >> To: r-help at r-project.org
>> >> >> Subject: [R] Need some help
>> >> >>
>> >> >>
>> >> >> Hi!
>> >> >> I'm taking a course that requires some programming background, but
>> I'm
>> >> a
>> >> >> complete novice in the field.
>> >> >>
>> >> >> when asked to generate a list of 20 uniform random numbers, is it
>> >> alright
>> >> >> if
>> >> >> I put in >randu, and just copy-paste the first 20 numbers?? Or is
>> >> there,
>> >> >> as
>> >> >> I suspect, a better way of calling out exactly 20 uniform random
>> >> >> numbers??
>> >> >>
>> >> > See ?runif
>> >> >
>> >> > rand_nums <- runif(20)
>> >> >
>> >> >> I'm also unable to solve the following problem:
>> >> >> We know that on average 30% of the customers who enter a store make
>> a
>> >> >> purchase. Suppose 200
>> >> >> people enter the store today. Run a simulation to see how many
>> >> purchases
>> >> >> we
>> >> >> will have today.
>> >> >>
>> >> > see ?sample
>> >> >> number_of_purchases <- sum(sample(c(0,1), 200, prob=c(.70, .30),
>> >> >> replace=TRUE))
>> >> >
>> >> > Hope this is helpful,
>> >> >
>> >> > Dan
>> >> >
>> >> > Daniel Nordlund
>> >> > Bothell, WA USA
>> >> >
>> >> > ______________________________________________
>> >> > R-help at r-project.org mailing list
>> >> > https://stat.ethz.ch/mailman/listinfo/r-help
>> >> > http://www.R-project.org/posting-guide.html
>> >> > and provide commented, minimal, self-contained, reproducible code.
>> >> >
>> >> >
>> >>
>> >> --
>> >> View this message in context:
>> >> http://www.nabble.com/Need-some-help-tf4624513.html#a13214128
>> >> Sent from the R help mailing list archive at Nabble.com.
>> >>
>> >> ______________________________________________
>> >> R-help at r-project.org mailing list
>> >> https://stat.ethz.ch/mailman/listinfo/r-help
>> >> http://www.R-project.org/posting-guide.html
>> >> and provide commented, minimal, self-contained, reproducible code.
>> >>
>> >
>> >
>> > --
>> > Jim Holtman
>> > Cincinnati, OH
>> > +1 513 646 9390
>> >
>> > What is the problem you are trying to solve?
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Need-some-help-tf4624513.html#a13216156
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help