Explicit Loop in R


R provides three ways to write loops:
  1.     For
  2.     Repeat
  3.     While




##For Loops
demodata<- LETTERS[1:6]
 for(item in demodata)
{
    print(item);
 }

##Repeat

rep(1:3,length.out=7)

 repeat
 {
          g <- rnorm(1)
          if (g > 1.0) break
          cat(g,"\n")
 }


##While
while (g < 1)
{
          g <- rnorm(1)
          cat(g,"\n")
}


Loop Control Statement
·         Break
·         Next


#Break
demodata<- LETTERS[1:6]
 for(item in demodata)
{
 if(item=="D")
 {
 break;
 }
 print(item);
 }

 #Next
 demodata<- LETTERS[1:6]
 for(item in demodata)
{
 if(item=="D")
 {
 next;
 }
 print(item);
 }
Explicit Loop in R Reviewed by Rupesh on 04:58 Rating: 5

No comments:

All Rights Reserved by Technology from Developers Eye © 2014 - 2015
Powered By Blogger, Designed by Aadics
Disclaimers:: The information provided within this blogsite is for general informational purposes only. While we try to keep the information up-to-date and correct, there are no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information, products, services, or related graphics contained in this blogsite for any purpose.The author does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.

Contact Form

Name

Email *

Message *

Powered by Blogger.