loop through all items in vector test and double them in r

You can loop through all items in a vector called "test" and double each item using a for loop and multiplication operator in R. Here is an example code block:

main.r
# create a vector called test
test <- c(1, 2, 3, 4)

# loop through all items in test and double them
for (i in 1:length(test)) {
  test[i] <- 2 * test[i]
}

# print the updated test vector
test
195 chars
11 lines

In this example, the original vector "test" contains the values 1, 2, 3, and 4. The for loop iterates through each item in the vector and multiplies it by 2 using the multiplication operator "*", and the updated value is assigned back to the same index in the vector using the bracket notation. Finally, the updated vector is printed to the console. The output should be the vector 2, 4, 6, 8.

gistlibby LogSnag