[R] for loop

(Ted Harding) Ted.Harding at wlandres.net
Mon Feb 13 20:12:50 CET 2012


In addition to Jim's neat solution (see also below),
some comments on your original code.

Your "for" loop executes x=29.5 + i/500 100 times,
producing a single value each time and replacing the
previous value which was in x. So, at the end of the loop,
you have a single value of x. Then you "compute" y=2x;
that, as it stands, would prokoke an error:

  Error: unexpected symbol in "y=2x"

since variable names cannort start with a digit. You
need, of course, the mutltiplaction operator "*" as
in Jim's "y <- 2 * x".

The scope of your "for" loop (i.e. the set of commands
that is executed for each round of the loop) is solely
the command "x=29.5 + i/500". The "y <- 2 * x" is not
part of the scope of the loop, and would only be executed
once, when the loop was finished. You would need

   for(i in 1:1000) {
     <commands>
   }

to cause the execution of several commands in each round
of the loop.

Finally, even if you did think that your entire series of
commands (re-written):

  for(i in 1:1000)
  x=29.5 + i/500
  y=2*x
  plot(y,x)

would all be executed (down to and including the plot() command)
in each round of the loop, nevertheless each call to plot()
creates a new graph, discarding the previous one, so only a single
point would be plotted each time.

The solution (as in Jim's suggestion) is to create the full vector
of x-values and y-values, and then use plot(x,y) where x and y are
now vectors.

There are all sorts of little details about how R puts things
together, which will become familiar as you use R. However,
you do need to get hold of the basics of how R operates, so
I would suggest having "R for Beginners"

  http://cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf

to hand while you learn R. It is very good about how the basics
work. The next step up would be the more systematic exposition
of how R works in  "An Introduction to R":

  http://cran.r-project.org/doc/manuals/R-intro.html
  http://cran.r-project.org/doc/manuals/R-intro.pdf

Hoping this helps!
Ted.

On 13-Feb-2012 jim holtman wrote:
> x <- 29.5 + (1:1000)/500
> y <- 2 * x
> plot(y,x)
> 
> On Mon, Feb 13, 2012 at 1:34 PM, eddie smith <eddieatr at gmail.com> wrote:
>> Hi guys,
>>
>> This is a very beginner question. Anybody willing to help?
>>
>> for(i in 1:1000)
>> x=29.5 + i/500
>> y=2x
>> plot(y,x)
>>
>> The idea is to produce 1000 values of x and y then plot them.
>>
>> Cheers,
>> Eddie
>>
>> _ _ _ _[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org 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.
> 
> 
> 
> -- 
> Jim Holtman
> Data Munger Guru
> 
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
> 
> ______________________________________________
> R-help at r-project.org 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.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 13-Feb-2012  Time: 19:12:47
This message was sent by XFMail



More information about the R-help mailing list