[R] Extracting elements from a list

2007-07-14 Thread Forest Floor
Hi, I would love an easy way to extract elements from a list. For example, if I want the first element from each of 10 arrays stored in a list, Lst[[1:10]][1,1] seems like a logical approach, but gives this error: Error: recursive indexing failed at level 3 The following workaround is

Re: [R] Extracting elements from a list

2007-07-14 Thread Adaikalavan Ramasamy
Try sapply( Lst, function(m) m[1,1] ) Also note that to subset a list, you just need Lst[ 1:10 ] and not Lst[[ 1:10 ]] (note the double square brackets). Regards, Adai Forest Floor wrote: Hi, I would love an easy way to extract elements from a list. For example, if I want the first

Re: [R] Extracting elements from a list

2007-07-14 Thread Gabor Grothendieck
Use lapply or sapply: L - list(a = 1:4, b = 11:15) lapply(L, [[, 1) $a [1] 1 $b [1] 11 sapply(L, [[, 1) a b 1 11 Also please see last line on every r-help message regarding providing reproducible code. Lst was not defined in your post. On 7/14/07, Forest Floor [EMAIL PROTECTED] wrote:

Re: [R] Extracting elements from a list

2007-07-14 Thread Prof Brian Ripley
The golden rule is that [[ ]] only returns one element: sapply(Lst, [, 1, 1) is probably what you want. On Sat, 14 Jul 2007, 'Forest Floor' aka 'rhago' aka 'Jeff' aka 'R User confused about his identity' wrote: Hi, I would love an easy way to extract elements from a list. For example, if