[R] How do I access a specific element of a multi-dimensional list
William Dunlap
wdunlap at tibco.com
Sat Feb 21 07:15:39 CET 2015
Using lapply() where Jim used sapply() would keep the types
right and be a fair bit faster than a solution based on repeatedly
appending to a list (like your getFirst).
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, Feb 20, 2015 at 1:52 PM, JS Huang <js.huang at protective.com> wrote:
> Hi,
>
> Jim's answer is neat. There is an issue on the result. All are
> characters even though some are numeric or logic. The following
> implementation retains the variable type.
>
> > x
> [[1]]
> [1] 2 3 5
>
> [[2]]
> [1] "aa" "bb" "cc"
>
> [[3]]
> [1] TRUE FALSE TRUE
>
> > getFirst
> function(aList)
> {
> result <- list()
> for (i in 1:length(aList))
> {
> result <- c(result, aList[[i]][1])
> }
> return(result)
> }
> > getFirst(x)
> [[1]]
> [1] 2
>
> [[2]]
> [1] "aa"
>
> [[3]]
> [1] TRUE
>
> >
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/How-do-I-access-a-specific-element-of-a-multi-dimensional-list-tp4703596p4703622.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list