r - vectorize head(which(t > x), n=1) for many values of x -


i have situation similar following in r:

t <- (1:100) * 15 x <- c(134, 552, 864, 5000) 

and want find each value in x first index in t t > x is. following works using loop:

y <- numeric(length(x)) (i in 1:length(x))     y[i] <- which(t > x[i])[1]  # y # [1]  9 37 58 na 

i taught loops in r 'bad , slow', , while time takes run reasonably large x not deal-breaker, know whether there better way?

fun <- function(x){     which(t > x)[1] }  r > sapply(x, fun) [1]  9 37 58 na 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -