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.hu...@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@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]] ______________________________________________ R-help@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.