带索引遍历数组

数组和集合 / 数组

numbers <- c(2, 3, 5, 7, 11, 13, 17)
string <- ""

# seq_along gives the indices 1, 2, ...
for (i in seq_along(numbers)) {
  if (i > 1) {
    string <- paste0(string, "; ")
  }
  string <- paste0(string, numbers[i])
  # The index lets you change the element
  numbers[i] <- numbers[i] * 10
}

cat("string =", string, "\n")
cat("numbers =", numbers, "\n")