R - For

Card Puncher Data Processing

About

The for flow construct.

Syntax

for(var in seq) expr

Example

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"

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"

Data Frame

dataFrame <- head(iris)
for (index in 1:nrow(dataFrame)) { 
  row = dataFrame[index, ]; 
  print(row[4])
} 





Discover More
Card Puncher Data Processing
R - Flow (Control Structure)

See The Control structures are primarily useful for writing programs. In the console, the apply functions (apply, sapply, ..) are most appropriated. if: testing a condition for: execute a loop n...
Card Puncher Data Processing
R - Loop Structure

The loop structures in R are : The loop can be stopped with the break statement. An iteration of a loop can be skipped with the next statement



Share this page:
Follow us:
Task Runner