[R] Noob question re: writing while loops on one line

Duncan Murdoch murdoch.duncan at gmail.com
Sun Feb 15 16:54:05 CET 2015


On 15/02/2015 10:08 AM, Sun Shine wrote:
> Thanks John: understanding it as a line return makes sense!

But it's not right.  This is one statement, and it returns the value 3:

1 +
2

This is an error:

1 + ; 2

The semicolon is a statement separator, not a line return.

Duncan Murdoch

> 
> Cheers
> 
> Sun
> 
> 
> On 15/02/15 14:59, John Kane wrote:
>> Hi Sun,
>> Can you check the code in the one line command in RStudio?
>>
>> I tied it and got the expected error.  Or to put it another way, it should not have run for you :)
>>
>> The semi-colon is funtioning as a line return
>>
>> John Kane
>> Kingston ON Canada
>>
>>
>>> -----Original Message-----
>>> From: phaedrusv at gmail.com
>>> Sent: Sun, 15 Feb 2015 10:55:28 +0000
>>> To: drjimlemon at gmail.com
>>> Subject: Re: [R] Noob question re: writing while loops on one line
>>>
>>> Brilliant Jim - that does the trick!!
>>>
>>> I guess then that the semi-colon rule works for any program or function
>>> that is being written on one line?
>>>
>>> Any reason why when writing this out in the RStudio source editor no
>>> semi-colon is required, but it is when written in the interactive
>>> console?
>>>
>>> Thanks again
>>>
>>> Sun
>>>
>>>
>>> On 15/02/15 10:41, Jim Lemon wrote:
>>>> Hi Sun,
>>>> Try including a semicolon.
>>>>
>>>> while(count < 10) { print(count); count<-count+1 }
>>>>
>>>> Jim
>>>>
>>>>
>>>> On Sun, Feb 15, 2015 at 9:20 PM, Sun Shine <phaedrusv at gmail.com> wrote:
>>>>> Hi list
>>>>>
>>>>> I'm working through some exercises and did a while loop which raised an
>>>>> issue for me:
>>>>>
>>>>> I can write out the while loop so:
>>>>>
>>>>>> count <- 0
>>>>> while(count < 10) {
>>>>>       print(count)
>>>>>       count <- count + 1
>>>>>      }
>>>>>
>>>>> And this works fine.
>>>>>
>>>>> Trying to do the same thing all on one line however gives this error:
>>>>>
>>>>> "Error: unexpected symbol in "while(count < 10) { print(count) count""
>>>>>
>>>>> My question:
>>>>>
>>>>> How can one write out a while loop all in one line? Is there a symbol
>>>>> or
>>>>> something that I should be including?
>>>>>
>>>>> Thanks for any suggestions.
>>>>>
>>>>> Sun
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> 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.
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>> ____________________________________________________________
>> FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
>> Visit http://www.inbox.com/photosharing to find out more!
>>
>>
>>
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>



More information about the R-help mailing list