Explicit Loop in R
R provides
three ways to write loops:
- For
- Repeat
- 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:
No comments: