create a loop that appends a random integer 1:10 to the end of an array in r

# Create an empty array
my_array <- c()

# Set the desired number of random integers to append
num_iterations <- 5

for (i in 1:num_iterations) {
  # Generate a random integer between 1 and 10
  random_integer <- sample(1:10, 1)
  
  # Append the random integer to the array
  my_array <- c(my_array, random_integer)
}

print(my_array)
336 chars
16 lines

related categories

gistlibby LogSnag