Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count occurrences of condition in lapply

Tags:

r

I am running a simulation that I need to keep track of number of occurrences in a function call of a particular condition. I attempted to accomplish this with an assignment to a global object. It works if you run the function but if you try to lapply the function as I'm doing then you get a single count of all the times the condition happened rather than a count for every time it happened for each element in the list fed to lapply.

Here's a dummy situation where the occurrence is evenness of a number:

FUN <- function(x){
    lapply(1:length(x), function(i) {
        y <- x[i]
        if (y %% 2 == 0){
            assign("count.occurrences", count.occurrences + 1, env=.GlobalEnv)   
        }
        print("do something")
    })
    list(guy="x", count=count.occurrences)
}

#works as expected
count.occurrences <- 0
FUN(1:10)


count.occurrences <- 0  
lapply(list(1:10, 1:3, 11:16, 9), FUN) 

#gives me...
#> count.occurrences
#[1] 9

#I want...
#> count.occurrences
#[1] 5  1  3  0

It's in a simulation so speed is an issue. I want this to be as fast as possible so I'm not married to the global assignment idea.

like image 753
Tyler Rinker Avatar asked Dec 26 '22 20:12

Tyler Rinker


1 Answers

Rather than assign to the global environment, why not just assign to inside FUN's environment?

FUN <- function(x){
    count.occurances <- 0
    lapply(1:length(x), function(i) {
        y <- x[i]
        if (y %% 2 == 0){
            count.occurances <<- count.occurances + 1
        }
        print("do something")
    })
    list(guy="x", count=count.occurances)
}

Z <- lapply(list(1:10, 1:3, 11:16, 9), FUN) 

Then you can just pull the counts out.

> sapply(Z, `[[`, "count")
[1] 5 1 3 0
like image 129
Brian Diggs Avatar answered Jan 05 '23 05:01

Brian Diggs