简体   繁体   中英

Using lapply() over a sequence of numbers

I would like to use lapply() in the following way:

I have a vector:

a<-c(1,4,6,8,9,10,11,12,14,19)
y<-1:10
lapply(a, function(x) othermatrix[othermatrix[,2,
    x] == somethingelse[y], 3, x])

The idea is to use the numbers specified in a instead of a usual sequence of consecutive numbers like 1:100 . but use a sequence of consecutive numbers as y

How can this be done?

You can use mapply :

a<-c(1,4,6,8,9,10,11,12,14,19)
y<-1:10

mapply(function(x,y) othermatrix[othermatrix[, 2, x] == somethingelse[y], 3, x], 
       a, y)

The first argument is the function. The other arguments are the objects passed to the function.

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