简体   繁体   中英

lapply over a list using it as input

I've got a list of things:

thing_list <- list("thing1","thing2","thing3")

And a function that takes one thing, and produces a result

function(thing){
    do operations with the thing
    return(result)
}

I want to apply the function to each element in the list. And store the result in a different list.

Here is where I have the doubt:

restul_list <- lapply(things_list, function(**HOW TO REFERENCE THE CURRENT THING AS ARGUMENT**))

How can I pass each element in the list as lapply goes by, as an argument for the function.

Thank you

If we want to get the value ( data.frame or vector or matrix , etc) stored in the objects, use mget to return a list of object values and then loop with lapply

 thing_list <-  c("thing1","thing2","thing3")
 lapply(mget(thing_list), yourfun)

Just do

lapply(thing_list, f)

Where f is your function.

result_list <- lapply(things_list, function(x) thing(x))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM