Access atomic vectors shown by Filter(is.atomic, eq) in R

前端 未结 3 764
悲哀的现实
悲哀的现实 2020-12-20 10:53
Filter(is.atomic, something)

returns atomic vectors.

1. Weather -example here

> Filter(is.atomic, stud         


        
相关标签:
3条回答
  • 2020-12-20 11:08

    To flatten a list so you can access the atomic vectors, you can use following function:

    flatten.list <- function(x){
      y <- list()
      while(is.list(x)){
        id <- sapply(x,is.atomic)
        y <- c(y,x[id])
        x <- unlist(x[!id],recursive=FALSE)
      }
      y
    }
    

    This function maintains names of the elements. Usage, using the list x from Vincent's answer :

    x <- list(
       list(1:3, 4:6),
       7:8,
       list( list( list(9:11, 12:15), 16:20 ), 21:24 )
    )
    

    then:

    > flatten.list(x)
    [[1]]
    [1] 7 8
    
    [[2]]
    [1] 1 2 3
    
    [[3]]
    [1] 4 5 6
    
    [[4]]
    [1] 21 22 23 24
    
    ...
    

    To recursively do an action on all atomic elements in a list, use rapply() (which is what Vincent handcoded basically).

    > rapply(x,sum)
    [1]  6 15 15 30 54 90 90
    
    > rapply(x,sum,how='list')
    [[1]]
    [[1]][[1]]
    [1] 6
    
    [[1]][[2]]
    [1] 15
    
    
    [[2]]
    [1] 15
    
    ...
    

    See also ?rapply

    PS : Your code Map(function(x) Filter(is.atomic, x), ls()) doesn't make sense. ls() returns a character vector, so every element of that character vector will be returned as part of the list. This doesn't tell you anything at all.

    Next to that, Filter() doesn't do what you believe it does. Taking the example list x, from the answer of Vincent, accessing only the atomic parts of it is pretty easy. Filter() only returns the second element. That's the only atomic element. Filter(is.atomic, x) is 100% equivalent to:

    ind <- sapply(x, is.atomic)
    x[ind]
    
    0 讨论(0)
  • 2020-12-20 11:20

    Filter will return a list. The functions lapply and sapply are typically used to process individual elements of a list object. If you instead want to access them by number using "[" or "[[" then you can determine the range of acceptable numbers with length(object). So object[[length(object)]] would get you the last element (as would ( tail(object, 1) ).

    0 讨论(0)
  • 2020-12-20 11:28

    Your question is very unclear, to say the least: an example of the input data you have and the desired output would help...

    Since you suggest that we "use our imagination", I assume that you have a hierarchical data structure, i.e., a list of lists of...of lists, whose depth is unknown. For instance,

    x <- list(
      list(1:3, 4:6),
      7:8,
      list( list( list(9:11, 12:15), 16:20 ), 21:24 )
    )
    

    The leaves are vectors, and you want to do "something" with those vectors.

    For instance, you may want to concatenate them into a single vector: that is what the unlist function does.

    unlist(x)
    

    You could also want all the leaves, in a list, i.e., a list of vectors. You can easily write a (recursive) function that explores the data structure and progressively builds that list, as follows.

    leaves <- function(u) {
      if( is.atomic(u) ) { return( list(u) ) }
      result <- list()
      for(e in u) {
        result <- append( result, leaves(e) )
      }
      return(result)
    }
    leaves(x)
    

    You could also want to apply a function to all the leaves, while preserving the structure of the data.

    happly <- function(u, f, ...) {
      if(is.atomic(u)) { return(f(u,...)) }
      result <- lapply(u, function(v) NULL) # List of NULLs, with the same names
      for(i in seq_along(u)) {
        result[[i]] <- happly( u[[i]], f, ... )
      }
      return( result )
    }
    happly(x, range) # Apply the "range" function to all the leaves
    
    0 讨论(0)
提交回复
热议问题