randomly select an odd-index element of an array and remove it in r

main.r
# Create an array
my_array <- c("apple", "banana", "cherry", "date", "elderberry", "fig")

# Get the odd-index elements
odd_index_elements <- my_array[seq(1, length(my_array), by = 2)]

# Select a random index of odd-index elements
selected_index <- sample(seq_along(odd_index_elements), 1)

# Remove the selected element from the original array
my_array <- my_array[-((seq(1, length(my_array), by = 2))[selected_index])]

# Result
print(my_array)
448 chars
15 lines

related categories

gistlibby LogSnag