for loop syntax in r code example
Example 1: r loops
for (i in sequence) {
code to be repeated
}
for (i in 1:5) {
print(i)
}
while (condition_is_true) {
code to be repeated
}
i = 1
while (i < 5) {
print(i)
i = i + 1
}
Example 2: r for loop
for (i in sequence) {
code to be repeated
}
for (i in 1:5) {
print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
for (i in 1:5) {
if (i == 2) {
next
}
if (i == 4) {
break
}
print(i)
}
[1] 1
[1] 3
Example 3: r for loop
for (val in sequence)
{
statement
}
Example 4: for in r
for (val in x) {
if(val %% 2 == 0) count = count+1
}