1

I am using lapply and mapply to create a bunch of operations. My data is composed of lists.

For one of the calculations I am having some problems on how to use any of the apply functions. This is an example of what I want to do:

#list with a collection of objects, each object has a vector with the their 2 closest neighbors identification. 
list_a<-list(c(2,4),c(1,3),c(1,4),c(3,2))

#list with the obs of each point
list_obs<-list(runif(1000,1,2),runif(1000,0,2),runif(1000,0.5,2),runif(1000,0.1,1.5))

position n in list_a corresponds to the neigh. points of point n

position n in list_obs corresponds to observations of point n

What I want to do is to create a new list that will store in each position n the list of observations of the neighb. points:

#output
 out=list(list(list_obs[[2]],list_obs[[4]]),list(list_obs[[1]],list_obs[[3]]),list(list_obs[[1]],list_obs[[4]]),list(list_obs[[3]],list_obs[[2]]))
> str(out)
List of 4
 $ :List of 2
  ..$ : num [1:1000] 1.673 1.423 0.228 1.758 1.65 ...
  ..$ : num [1:1000] 0.679 1.341 0.148 0.867 0.724 ...
 $ :List of 2
  ..$ : num [1:1000] 1.25 1.5 1.58 1.54 1.6 ...
  ..$ : num [1:1000] 0.526 1.545 1.848 0.711 0.697 ...
 $ :List of 2
  ..$ : num [1:1000] 1.25 1.5 1.58 1.54 1.6 ...
  ..$ : num [1:1000] 0.679 1.341 0.148 0.867 0.724 ...
 $ :List of 2
  ..$ : num [1:1000] 0.526 1.545 1.848 0.711 0.697 ...
  ..$ : num [1:1000] 1.673 1.423 0.228 1.758 1.65 ...

Can anyone advise me on the best way to do this?

My data is huge, so a loop will take 2 much time to run. The data is composed of 3000 points (with 3000-1 neigh.), each point with +20.000 observations.

2
  • As you describe your data it could be in a matrix, 2 columns on left for neighbours, and 1000 columns of observations. That might be simpler. On loops it's a myth that for loops are (implicitly) slower than built-in like apply, mapply although the way you do it may lead to inefficiencies. some things like plyr maybe quicker.. example below.. you might try dput(head(somedata, 50)) for a reproducible example.. Commented Nov 27, 2013 at 7:46
  • I'm not aware there is a maximum matrix size other than memory, and I don't see why a list uses less memory or is easier to handle than a matrix of the same elements. If the data really is huge you might consider putting it in rsqlite and/or using the dplyr package. Commented Nov 27, 2013 at 8:07

2 Answers 2

1

Have you tried the following command?

lapply(list_a, function(x) list_obs[x])
Sign up to request clarification or add additional context in comments.

Comments

1
lapply(list_a,FUN=function(x)lapply(x,FUN=function(y)list_obs[y]))

but, as per comments, there might be a better way to do it

sorry saw that was just answered!

lapply(list_a,FUN=function(x)list_obs[x]) 

is better!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.