예를 들어 i 변수를 사용하여 1:3 벡터를 순차적으로 반복하는 루프도 만들 수 있다. 반복할 때마다 i 값을 화면에 출력한다.
> for (i in 1:3) { + cat("The value of i is", i, "\n") + } The value of i is 1 The value of i is 2 The value of i is 3
이러한 반복문은 수치형 벡터에만 적용되는 것이 아니라 모든 벡터에서 가능하다. 예를 들어 정수형 벡터 1:3을 문자형 벡터로 바꿀 수 있다.
> for (word in c("hello","new", "world")) { + cat("The current word is", word, "\n") + } The current word is hello The current word is new The current word is world