Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save StudioEtrange/572b786e28e897c6b563 to your computer and use it in GitHub Desktop.
Save StudioEtrange/572b786e28e897c6b563 to your computer and use it in GitHub Desktop.
R : browse recursivly each item of a complex list (which include list) and apply operation to each leave
# http://stackoverflow.com/questions/30487010/recursively-apply-function-to-list-elements
recursive_browse_list <- function(x) {
  if(is.list(x)) {
   
    # (OPTIONAL) DO HERE SOME OPERATION ON LIST 
    # example here : add names to list which do not have name
    if(is.null(names(x))) {
      if(length(x)>0) names(x) <- as.list(1:length(x))
    }
    
    lapply(x, recursive_browse_list)
    
  } else {
    # DO HERE SOME OPERATION ON x
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment