take a list and find the maximum element in every 10 element window in r

To find the maximum element in every 10 element window of a list in R, you can use a loop and the max() function. Here's an example:

main.r
# create a list with 100 elements
my_list <- list(sample(1:100, 100, replace = TRUE))

# create empty vector to store max values
max_values <- vector()

# loop over every 10 element window in list
for (i in seq(1, length(my_list[[1]]), by = 10)) {
  # get the maximum value in the window
  max_val <- max(my_list[[1]][i:(i+9)])
  # add the maximum value to the vector
  max_values <- c(max_values, max_val)
}

# print the vector of max values
max_values
454 chars
17 lines

In this example, we first create a list with 100 random integer elements. We then create an empty vector called max_values to store the maximum values we find. Next, we use a loop to iterate over every 10 element window in the list.

Within the loop, we use the max() function to find the maximum value in the current window, and then add that maximum value to our max_values vector using the c() function.

After the loop is finished, we print out the max_values vector to see the maximum value in every 10 element window.

related categories

gistlibby LogSnag