vrige
vrige

Reputation: 319

R vectorization without ifelse

a <- array(1:18, dim=c(3,3,2))
a1 <- which( a >= 17, arr.ind = T)
a1im <- cbind(a1[,1] - 1, a1[,c(2,3)])
#ADVDOMiM is a logical vector

I would like a better vectorization than this one:

a[a1im] <- ifelse( a[a1] >= 5 & ADVDOMiM, a[a1], a[a1im])

I would like something like this:

 a[a[a1] >= 5 & ADVDOMiM] <- a[a1]

but it doesn't work( i need to change the result in a[a1im], but i don't know how to do it)

Upvotes: 0

Views: 55

Answers (1)

GKi
GKi

Reputation: 39667

You can try to subset a1im and a1 with a[a1] >= 5 & ADVDOMiM to avoid the ifelse:

i <- a[a1] >= 5 & ADVDOMiM
a[a1im[i,]] <- a[a1[i,]]

Upvotes: 1

Related Questions