[R] How do I access a specific element of a multi-dimensional list

Huang, JS JS.Huang at protective.com
Mon Feb 23 13:47:54 CET 2015


Bill,

   Thank you for providing the neat and fast solution.

JS Huang
636.536.5635

From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: Saturday, February 21, 2015 12:16 AM
To: Huang, JS
Cc: r-help at r-project.org
Subject: Re: [R] How do I access a specific element of a multi-dimensional list

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<http://tibco.com>

On Fri, Feb 20, 2015 at 1:52 PM, JS Huang <js.huang at protective.com<mailto: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<mailto: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.



Confidentiality Notice: This e-mail communication and any
attachments may contain confidential and privileged information for
the use of the designated recipients named above. If you are not
the intended recipient, you are hereby notified that you have
received this communication in error and that any review,
disclosure, dissemination, distribution or copying of it or its
contents is prohibited. If you have received this communication in
error, please notify me immediately by replying to this message and
deleting it from your computer. Thank you.
	[[alternative HTML version deleted]]



More information about the R-help mailing list