rnorouzian
rnorouzian

Reputation: 7517

Combined subsetting in R?

I trying to subset 3 ys for when xs are -1, 0, and 1 in my code below. But I was hoping to do this all at once using y[c(x == -1, x == 0, x == 1)] which apparently does not work (see below).

Any better way to do this subsetting all at once?

x = seq(-1, 1, l = 1e4)
y = dcauchy(x, 0, sqrt(2)/2)
y[c(x == -1, x == 0, x == 1)] ## This subsetting format doesn't work 

Upvotes: 0

Views: 49

Answers (1)

www
www

Reputation: 39174

We can do this.

y[x == -1| x == 0| x == 1]

Or this

y[x %in% c(-1, 0, 1)]

Upvotes: 2

Related Questions