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

2015-02-20 Thread William Dunlap
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 wrote: > Hi, > > Jim's answer is neat

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

2015-02-20 Thread JS Huang
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 <- li

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

2015-02-20 Thread jim holtman
try this: > a = c(2, 3, 5) > b = c("aa", "bb", "cc") > c = c(TRUE, FALSE, TRUE) > > x = list(a, b, c) > x [[1]] [1] 2 3 5 [[2]] [1] "aa" "bb" "cc" [[3]] [1] TRUE FALSE TRUE > sapply(x, '[[', 1) [1] "2""aa" "TRUE" > Jim Holtman Data Munger Guru What is the problem that you are trying t

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

2015-02-20 Thread Knut Hansen
Dear list, Let's say I have setup the following list: a = c(2, 3, 5) b = c("aa", "bb", "cc") c = c(TRUE, FALSE, TRUE) x = list(a, b, c) I want to access the first second dimension element of each first dimension element so that the result is something like: (2, "aa", TRUE) In my real life p