0

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 
rnorouzian
  • 7,029
  • 5
  • 22
  • 57

1 Answers1

2

We can do this.

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

Or this

y[x %in% c(-1, 0, 1)]
www
  • 37,164
  • 12
  • 37
  • 72