extract xyz values from a list of lists in r

Do you need something like this ?

get_elements <- function(data, i) {
  #select the main list
  tmp <- data[[i]]
  #Check the length of each sublist, select minimum value
  #and sample 1 number from 1 to that number
  rand_int <- sample(min(lengths(tmp)), 1)
  #select that element from each sub-list
  sapply(tmp, `[[`, rand_int)

get_elements(data, 1)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top