[R] how to fast extract values from different list elements

kMan kchamberln at gmail.com
Sat Feb 27 03:23:20 CET 2010


Dear Peter,

What data types does your list contain? Have you tried treating the list as
a data frame or matrix? 

KeithC.

-----Original Message-----
From: Heym, Peter-Paul [mailto:pheym at ipb-halle.de] 
Sent: Thursday, February 25, 2010 2:11 AM
To: r-help at R-project.org
Subject: [R] how to fast extract values from different list elements

hi,

I have a list L having more than 14000 Elements, each of these contains an
array of about length 1200.

> L[[1]][26:30] # e.g. print 5 entries of first element of L
[1]   0.0000000   6.7982652 114.4737184  89.7328239   3.2001664

Furthermore I get two arrays A and B of same length as input.

A<-c(4,7,9,34,463,788)
B<-c(50,67,87,361,45,89)

I would like to extract (or print or save) certain values of L which I do in
the following (inefficient) way at the moment:

for (i in 1:length(A) {
	print( L[[A[i]]][B[i]] ) }

this works fine but it is very slow (since A and B can be very large and I
have to repeat this about 5000 times). I would like to make this faster
using e.g. apply or lapply but I didn't get it work using these methods.
Does anybody know an EFFICIENT or FAST way extract the values from L using
the values from A and B?

thanks for your answers.
Peter

	[[alternative HTML version deleted]]



More information about the R-help mailing list