Reputation: 854
So I am new to R (I come from a Python background) and I am still having some issues understanding how/when to implement apply
functions (lapply, sapply, rapply, etc) instead of nested loops.
As an example, suppose you wanted to perform some function FUN that compared each element of list to each element of another list. I would write something along the lines of:
n = 1
m = 1
sameList = NULL
for(i in 1:length(list1)){
for(j in 1:length(list2)){
if(list1[n]==list2[m]){
sameList<-c(sameList, list1[n]}
n = n+1
}
m = m+1
}
In other words, some nested loop that iterates over every element of each list.
What I am learning is that concatenating a list mid-loop is a very inefficient process in R, which is why apply
is used.
So how would apply
(or any version of it) be used to replace the above example code?
Upvotes: 7
Views: 8422
Reputation: 17432
To use lapply
, you would run:
sameList = lapply(list1, function(x) lapply(list2, function(y) if (x==y) x else NULL))
Upvotes: 8
Reputation: 2414
For this specific case, I would use
sameList <- intersect(list1, list2)
Also, the terminology in R means we use vector
for collections of one type; list
s are allowed to contain different types.
Avoidance of loops is perhaps an aesthetic issue, as performance of loops compared to apply is better than it used to be - but the performance degradation you're noticing is probably due more to constantly changing the size of sameList (and the consequent reallocation of memory) than anything inherently loopy about your code.
Upvotes: 1