momeara
momeara

Reputation: 1381

R array manipulation

In python lists can be sliced like this x[4:-1] to get from the fourth to the last element.

In R something similar can be accomplished for vectors with x[4:length(x)] and for multidimensional arrays with something like x[,,,,4:dim(x)[5],,,]. Is this more elegant syntax for array slicing for a particular dimension from an element in the middle to the last element?

Thanks

Upvotes: 14

Views: 20358

Answers (2)

Xavier Guardiola
Xavier Guardiola

Reputation: 2799

In case you are interested in slicing the last n elements of the array then you could use:

x[seq(length=n, from=length(x), by=-1)] 

Upvotes: 7

AnthonyF
AnthonyF

Reputation: 888

You could use the drop elements syntax:

> (1:10)[-(1:4)]
[1]  5  6  7  8  9 10

Upvotes: 24

Related Questions