0

I am having trouble with what seems to be a very basic R statement:

while (count < 10) { print(count) count <- count + 1}
Error: unexpected symbol in "while (count < 10) { print(count) count"

What is causing the error?

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
Misha
  • 1
  • 1
  • 4
    Perhaps you need `;` `while (count < 10) { print(count); count <- count + 1}` – akrun Oct 25 '14 at 14:42
  • Yes that seemed to work just fine! Thank you for your input. As a matter of best practice, do you think it would be to my benefit to create new lines in between the statements? – Misha Oct 25 '14 at 14:47
  • 2
    Yes. You should definitely choose and stick to a style. I adhere (mostly) to [Google's R Style Guide](https://google-styleguide.googlecode.com/svn/trunk/Rguide.xml). Semicolons (like pie charts) are evil. – hrbrmstr Oct 25 '14 at 14:53
  • Yes, new statements on new lines. And indentation too. It makes code more readable for others and will make debugging easier for you – Rich Scriven Oct 25 '14 at 15:32
  • Thanks all for the feedback. I really appreciate it. I'm such a rookie! – Misha Oct 25 '14 at 15:39
  • possible duplicate of [Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code](http://stackoverflow.com/questions/25889234/error-unexpected-symbol-input-string-constant-numeric-constant-special-in-my-co) – Henrik Oct 25 '14 at 20:58

1 Answers1

0

Generally I would put the "more than simple call" on multiple lines as follows:

while (count < 10) { 
    print(count) 
    count <- count + 1
}

This makes what you're doing clearer and avoids problems like you're getting.

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519