for (i in 10:1) {
print(i)
}
As easy as that.
Tags: loops
This entry was posted on Saturday, March 20th, 2010 at 1:02 pm and is filed under feature, r.
You can follow any comments to this entry through the RSS 2.0 feed.
Both comments and pings are currently closed.
Thx!
Input
T<-10
for (t in (T-1:1)){
print("yes")
for (s in (1:10)){
print(s)
}
}
Output:
yes
1 to 10 printed…
Thus, outer loop is executed only once. I don't know what Im missing.
If you haven’t figured it out yet, it is because the brackets surround the colon operator in the first for loop i.e. (T-1:1).
Assuming the brackets are there to denote precedence, the first for loop should be:
for(t in (T-1):1){
print(“yes”)
…
}
That should print 9 “yes”. Hope this helps.