Reputation: 1294
How can I remove the first and last 5% of elements of a sorted vector?
Upvotes: 0
Views: 4358
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
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