R - For

> Procedural Languages > R

1 - About

The for flow construct.

Advertising

3 - Syntax

for(var in seq) expr

4 - Example

4.1 - Sequence

for(i in 1:10) { print(c(i,"Hello World")) }
[1] "1"           "Hello World"
[1] "2"           "Hello World"
[1] "3"           "Hello World"
[1] "4"           "Hello World"
[1] "5"           "Hello World"
[1] "6"           "Hello World"
[1] "7"           "Hello World"
[1] "8"           "Hello World"
[1] "9"           "Hello World"
[1] "10"          "Hello World"

4.2 - Vector

v=c("Nico","is","my","Name")
for (word in v) { print(word)}
# of
for(i in seq_along(v)) { print(v[i]) }
[1] "Nico"
[1] "is"
[1] "my"
[1] "Name"

4.3 - Data Frame

dataFrame <- head(iris)
for (index in 1:nrow(dataFrame)) { 
  row = dataFrame[index, ]; 
  print(row[4])
} 
lang/r/for.txt · Last modified: 2017/04/10 17:52 by gerardnico