Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R equivalent to the MATLAB structure?

Tags:

r

matlab

Is there an R type equivalent to the Matlab structure type?

I have a few named vectors and I try to store them in a data frame. Ideally, I would simply access one element of an object and it would return the named vectors (like a structure in Matlab). I feel that using a data frame is not the right thing to do since it can store the values of the named vectors but not the names when they differ from one vector to the other.

More generally, is it possible to store a bunch of different objects in a single one in R?

Edit: As Joran said I think that list does the job.

l = list()
l$vec1 = namedVector1
l$vec2 = namedVector2
...

If I have a list of names

name1 = 'vec1'
name2 = 'vec2'

is there any way for the interpreter to understand that when I use a variable name like name1, I am not referring to the variable name but to its content? I have tried get(name1) but it does not work.

like image 460
Youcha Avatar asked Jun 25 '12 23:06

Youcha


People also ask

What is a MATLAB structure?

Structures and cell arrays are two kinds of MATLAB arrays that can hold generic, unstructured heterogeneous data. A structure array is a data type that groups related data using data containers called fields. Each field can contain any type of data.

What is the equivalent of a MATLAB structure in Python?

NumPy (Numerical Python)NumPy arrays are the equivalent to the basic array data structure in MATLAB.

Can a structure contain a structure MATLAB?

Structure Cannot Contain Pointers to Other Structures Nested structures or structures containing a pointer to a structure are not supported. However, MATLAB can access an array of structures created in an external library.


1 Answers

I could still be wrong about what you're trying to do, but I think this is the best you're going to get in terms of accessing each list element by name:

l <- list(a= 1:3,b = 1:10)
> ind <- "a"
> l[[ind]]
[1] 1 2 3

Namely, you're going to have to use [[ explicitly.

like image 111
joran Avatar answered Sep 30 '22 11:09

joran