get_valLL {lazy.tools}R Documentation

Retrieve the Elements Contained in the Lists in a List

Description

This version is very slow!!

Usage

get_valLL(
  L,
  name,
  removenull = 0,
  simplify = 1,
  removeNull = 0,
  method = 1,
  debug = 0
)

Arguments

L

A list of lists or data frames

name

The name of the list element, or the element number.
Can be a vector.

removenull

= 1 to remove NULL elements.

simplify

= 0 not to simplify the result.
= 1 to simplify the result if possible
= 2 to simplify the result if possible when name is a vector.
Only works with method=1.

removeNull

= 1 to remove all NULL columns.

method

= 2 to use new method with sapply

debug

= 1 to print the details of method=2

Details

It is assumed that L is a list consisting of several lists which contain the same objects.

Specifying method=2 is faster but works only when:
1) name is a scalar and the elements to be retrieved are scalars or vectors with no missing elements, not matrices or lists.
The result will be a vector or matrix. 2) name is a vector and the elements to be retrieved are scalars.
The result will be a matrix.

If the above conditions are not met, method=1 will be used.

Note that, Specifying method=2 is equivalent to
res <- sapply( L, function(x) x[[name]] )

When method=1:

If simplify=1 is given, the result will be converted to a data frame or matrix if possible.

If the L contains lists or arrays, or when name is a vector, the result will not be simplified.

Try l2a( lapply( "get_valLL result", a2m, method=2 ) ) .

If removenull=1 is given, and name is not valid, NULL or named list() may be returned.

If the L consists of data frames only, better use get_valL.

removeNull=1 may be needed when not all the lists in L have the same set of elements. It can always be set equal to 1 but not thoroughly tested yet.

Value

A list, data frame, matrix or vector consisting of the specified elements.

Examples

# case where all the lists consists of the objects of the same size.
set.seed(1701)
n <- 100; nvar <- 3
ngroup <- 3
X <- matrix(rnorm(n*nvar)/100,n,nvar)
group <- sample(1:ngroup,n,replace=1)
X <- group+X
group <- letters[group]
md <- mandd(X,by=group)

get_valLL(md,"mean")
get_valLL(md,"mean", simplify=0)
get_valLL(md,c("mean","cov"))


# univariate case with simplify=2
md2 <- lapply(md, function(x){lapply(x,"[",1)})
get_valLL(md2,"mean")
get_valLL(md2,"mean", simplify=0)
get_valLL(md2,c("mean","std"))
get_valLL(md2,c("mean","std"), simplify=2)


# general case
L0 <- list( A=(demomat(3,2)*10+1)
            , B=(1:3)*10+2
            , C=data.frame(demomat(5,4)*10+3)
)
L00 <- list( A=(demomat(3,2)*100+1)
             , B=(1:3)*100+2
             , C=(demomat(5,4, root="c"))
             , D=(demomat(5,4, root="d"))
)
L000 <- list( A=(demomat(3,2)*1000+1)
              , B=(1:3)*1000+2
              , C2=(demomat(5,4)*1000+3)
              , D=(demomat(4,5)*1000+4)
)
L <- list(L0,L00,L000)
names(L) <- c("a","b","c")

get_valLL( L, "A" ) #   This is a list of matrices.
get_valLL( L, "B" ) #   This is a matrix.
get_valLL( L, "B", simplify=0 ) #   This is a list of vectors.
get_valLL( L, "C", simplify=0 ) # NULL indicates missing elements.
get_valLL( L, "D" )  # same as get_valLL( L, 4 )
get_valLL( L, 4, removenull=1  ) # missing elements were removed.


# example of removeNull
L21 <- list( a=1, b=1, c=1 )
L22 <- list( a=2, c=2 )
L23 <- list( a=3, b=3, c=3 )
L2 <- list( x=L21, y=L22, z=L23 )
( get_valLL( L2, "b" ) )
( get_valLL( L2, "b", removeNull=1 ) )




#' # Example of \code{method=2}

# mean and std are vectors
get_valLL(md,"mean", method=2, debug=1)
get_valLL(md,"cov", method=2, debug=1) # --> method=1
get_valLL(md,c("mean","std"), method=2, debug=1, simplify=2) # -> method=1


# mean and std are scalar
md2 <- lapply(md, function(x){lapply(x,"[",1)})
get_valLL(md2,"mean", method=2, debug=1)
get_valLL(md2,c("mean","std"), method=2, debug=1)
get_valLL(md2,c("mean","std"), method=1, debug=1, simplify=2)


# general case: B is a scalar
L0 <- list( A=(demomat(3,2)*10+1)
            , B=(1:3)*10+2
            , C=data.frame(demomat(5,4)*10+3)
)
L00 <- list( A=(demomat(3,2)*100+1)
             , B=(1:3)*100+2
             , C=(demomat(5,4, root="c"))
             , D=(demomat(5,4, root="d"))
)
L000 <- list( A=(demomat(3,2)*1000+1)
              , B=(1:3)*1000+2
              , C2=(demomat(5,4)*1000+3)   # C is missing.
              , D=(demomat(4,5)*1000+4)
)
L <- list(L0,L00,L000)
names(L) <- c("a1","b2","c3")

get_valLL( L, "A", method=2, debug=1 ) # --> method=1
get_valLL( L, "B" , method=2, debug=1)
get_valLL( L, "C", simplify=0, method=2, debug=1 ) # C is NULL at the 3rd list
get_valLL( L, 2, removenull=1, method=2, debug=1  ) # same as "B"


# example of missing list elements
L21 <- list( A=1, B=1, C=1 )
L22 <- list( A=2, C=2 )        # B is missing.
L23 <- list( A=3, B=3, C=3 )
L2 <- list( x=L21, y=L22, z=L23 )
get_valLL( L2, "B", method=2, debug=1 )
get_valLL( L2, c("A","B"), method=2, simplify=2, debug=1 )


# difference between method=1 and 2
get_valLL(md2,"mean", method=1)
get_valLL(md2,"mean", method=2)

get_valLL(L2,"B", method=1, removeNull=1)
get_valLL(L2,"B", method=2)



[Package lazy.tools version 0.1.6 ]