-
Notifications
You must be signed in to change notification settings - Fork 30
/
150_list.Rmd
executable file
·32 lines (20 loc) · 1.27 KB
/
150_list.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# List
This chapter explains how to create `List` object, how to access its elements, and its member functions. In Rcpp, `List` is implemented as a kind of vector. In other words, `Vector` is a vector whose element is scalar value, while `List` is a vector whose elements are any kinds of data types. Therefore, `Vector` and `List` generally have common member functions.
Since the contents described on the page of [DataFrame](140_dataframe.html) can be mostly valid for `List` if replacing `DataFrame` with `List`, please refer to that for details.
## Creating List object
To create a `List` object we use the `List::create()` function. Also, to specify the element name when creating `List`, use `Named()` function or `_[] `.
```cpp
// Create list L from vector v1, v2
List L = List::create(v1, v2);
// When giving names to elements
List L = List::create(Named("name1") = v1 , _["name2"] = v2);
```
## Accessing List elements
When accessing a specific element of `List`, we assign it to the other object and access it via that object.
The elements of `List` can be specified by numerical index, element names and logical vector.
```cpp
NumericVector v1 = L[0];
NumericVector v2 = L["V1"];
```
## Member functions {#member-functions-list}
`List` has the same member functions as `Vector`.