문제

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?

도움이 되었습니까?

해결책

To use lapply, you would run:

sameList = lapply(list1, function(x) lapply(list2, function(y) if (x==y) x else NULL))

다른 팁

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; lists 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top