Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 923
momeara Avatar asked Jan 23 '10 17:01

momeara


People also ask

How do you store data in an array in R?

Creating an ArrayAn array in R can be created with the use of array() function. List of elements is passed to the array() functions along with the dimensions as required. dimnames : Default value = NULL. Otherwise, a list has to be specified which has a name for each component of the dimension.

How do I create a multidimensional array in R?

Creating a Multidimensional ArrayAn array is created using the array() function. It takes vectors as input and uses the values in the dim parameter to create an array. A multidimensional array can be created by defining the value of 'dim' argument as the number of dimensions that are required.

What is array manipulation?

Array Type Manipulation in C++ The array is a data structure in c++ that stored multiple data elements of the same data type in continuous memory locations. In c++ programming language, there are inbuilt functions to manipulate array types. Some functions can also be applied to multidimensional arrays.

How do I create an array of vectors in R?

We can create an array using array() function. We have to pass the vectors and dim() as parameters. Here dim() function is used to give dimensions for the array. The approach is straightforward first we need to create vectors and then pass them to the array() function to create one.


1 Answers

You could use the drop elements syntax:

> (1:10)[-(1:4)]
[1]  5  6  7  8  9 10
like image 149
AnthonyF Avatar answered Sep 24 '22 03:09

AnthonyF