R array manipulation

心不动则不痛 提交于 2020-01-12 13:51:55

问题


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


回答1:


You could use the drop elements syntax:

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



回答2:


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)] 


来源:https://stackoverflow.com/questions/2123968/r-array-manipulation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!