Math
Math

Reputation: 1294

R removing the first and last 5% of elements of a vector

How can I remove the first and last 5% of elements of a sorted vector?

Upvotes: 0

Views: 4358

Answers (2)

dirkster
dirkster

Reputation: 522

Remember vector indices in R start at 1, so you'll want

x <- round(0.05*length(v))
v[(x+1) : (length(v)-x)]

Upvotes: 4

Zheyuan Li
Zheyuan Li

Reputation: 73315

OK, since your vector x is already sorted, you can do

x[round(0.05 * length(x)) : round(0.95 * length(x))]

It does not matter whether x is ascending or descending.

Upvotes: 3

Related Questions