[R] Simple Loop Counter

arun smartpink111 at yahoo.com
Sat Apr 26 10:45:40 CEST 2014


HI,
May be this helps:
for(i in letters) {
  n <- n+1
  x[n,] <- c(i, n)
cat("loop", n, "\n")
}
x
#or
for(i in seq_along(letters)) {
  n <- n+1
  x[n,] <- c(letters[i], n)
cat("loop", i, "\n")
}
x

A.K.


#How can I add the loop counter in #2 to the loop in #1.?
#***************
#1.
x <- matrix( , ncol = 2, nrow = 26) # empty matrix
n <- 0     #set n to 0
for(i in letters) {
  n <- n+1
  x[n,] <- c(i, n)
}
x
#***********
#2.
for (i in 1:10) {
cat("loop", i, "\n")
}




More information about the R-help mailing list