% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_valLL.R
\name{get_valLL}
\alias{get_valLL}
\title{Retrieve the Elements Contained in the Lists in a List}
\usage{
get_valLL(
  L,
  name,
  removenull = 0,
  simplify = 1,
  removeNull = 0,
  method = 1,
  debug = 0
)
}
\arguments{
\item{L}{A list of lists or data frames}

\item{name}{The name of the list element, or the element number. \cr
Can be a vector.}

\item{removenull}{= 1 to remove NULL elements.}

\item{simplify}{= 0 not to simplify the result. \cr
= 1 to simplify the result if possible \cr
= 2 to simplify the result if possible when name is a vector. \cr
Only works with \code{method=1}.}

\item{removeNull}{= 1 to remove all NULL columns.}

\item{method}{= 2 to use new method with sapply}

\item{debug}{= 1 to print the details of \code{method=2}}
}
\value{
A list, data frame, matrix or vector consisting of the specified elements.
}
\description{
This version is very slow!!
}
\details{
It is assumed that \code{L} is a list consisting of several lists
which contain the same objects.

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

If the above conditions are not met, \code{method=1} will be used.

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


When \code{method=1}: \cr

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

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

Try \code{l2a( lapply( "get_valLL result", a2m, method=2 ) )} .

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

If the \code{L} consists of data frames only, better use \code{get_valL}.

\code{removeNull=1} may be needed when not all the lists in \code{L}
have the same set of elements.
It can always be set equal to 1 but not thoroughly tested yet.
}
\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)


}
