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
Post a Comment