# Collections and Data Structures

## Lists

Lists in R are ordered collections of data that can be of different classes.&#x20;

### Creating Lists

| Action           | Syntax                            |
| ---------------- | --------------------------------- |
| New list (empty) | listname <- list()                |
| New list (misc)  | listname <- list(1L, "abc", 10.3) |

### Accessing List Elements

<table><thead><tr><th width="342">Action</th><th>Syntax</th></tr></thead><tbody><tr><td>Access an element</td><td>list[position]</td></tr><tr><td>Change a value</td><td>list[position] &#x3C;- newvalue</td></tr><tr><td>See number of values in a list</td><td>length(list)</td></tr><tr><td>See if item is present in a list</td><td>item %in% list</td></tr></tbody></table>

### Adding and Removing List Elements

| Action                                    | Syntax                             |
| ----------------------------------------- | ---------------------------------- |
| Add item to a list                        | append(list)                       |
| Add item to a list at a specific position | append(list, after=*index number*) |
| Remove item from list                     | newlist <- list\[-*index number]*  |

#### Inputs:

```r
#Create list
mylist <- list("apple", "peach", "plum")

#Access the second element of a list
mylist[2]

#Change the value of the first element of a list
mylist[1] <- "banana"
mylist

#See the number of values in a list
length(mylist)

#Check if item exists in list
"plum" %in% mylist

#Add an item to the list
append(mylist, "orange", after=2)
mylist

#Remove an item at index=3 from a list
mylist <- list("apple", "peach", "plum")
newlist <- mylist[-3]
newlist
```

#### Outputs:

```r
#Access the second element of a list
"peach"

#Change the value of the first element of a list
[[1]]
[1] "banana"

[[2]]
[1] "peach"

[[3]]
[1] "plum"

#See the number of values in a list
3

#Check if item exists in list
TRUE

#Add an item to the list
[[1]]
[1] "banana"

[[2]]
[1] "peach"

[[3]]
[1] "orange"

[[4]]
[1] "plum"

#Remove an item from a list
[[1]]
[1] "apple"

[[2]]
[1] "peach"
```

## Matrices

### Creating Matrices

<table><thead><tr><th width="248">Action</th><th>Syntax</th></tr></thead><tbody><tr><td>New matrix (empty)</td><td>matrixname &#x3C;- matrix()</td></tr><tr><td>New matrix (numbers)</td><td>matrixname &#x3C;- matrix(data, nrow=, ncol=)</td></tr><tr><td>New matrix (strings)</td><td>matrixname &#x3C;- matrix(data, nrow=, ncol=)</td></tr></tbody></table>

### Accessing Matrix Elements

| Action                  | Syntax                                 |
| ----------------------- | -------------------------------------- |
| Access a matrix element | matrix\[row position, column position] |
| Access an entire row    | matrix\[row position,]                 |
| Access an entire column | matrix\[,column position]              |

### Adding and Removing Matrix Elements

| Action                      | Syntax                               |
| --------------------------- | ------------------------------------ |
| Create an additional row    | rbind(matrix, values for new row)    |
| Create an additional column | cbind(matrix, values for new column) |

#### Inputs:

```r
#Creating array
heart <- matrix(c("left atrium", "left ventricle", 
    "right atrium", "right ventricle"), nrow=2, ncol=2)
heart

#Access element at row=1, column=2
heart[1,2]

#Access entire row 1
heart[1,]

#Access entire column 2
heart[,2]

#Create new row
heart1 <- rbind(heart, c("x", "x"))
heart1

#Create new column
heart2 -< cbind(heart1, c("y", "y", "z"))
heart2
```

#### Outputs:

```r
#Creating array
     [,1]           [,2]      
[1,] "left atrium"    "right atrium"   
[2,] "left ventricle" "right ventricle"

#Access element at row=1, column=2
"right atrium"

#Access entire row 1
"left atrium"  "right atrium"

#Access entire column 2
"right atrium" "right ventricle"

#Create new row
     [,1]             [,2]             
[1,] "left atrium"    "right atrium"   
[2,] "left ventricle" "right ventricle"
[3,] "x"              "x"   

#Create new column
     [,1]             [,2]              [,3]
[1,] "left atrium"    "right atrium"    "y" 
[2,] "left ventricle" "right ventricle" "y" 
[3,] "x"              "x"               "z" 
```

## Arrays

### Creating Arrays

<table><thead><tr><th width="253">Action</th><th>Syntax</th></tr></thead><tbody><tr><td>New array (empty)</td><td>arrayname &#x3C;- array()</td></tr><tr><td>New array (numbers)</td><td>arrayname &#x3C;- array(data, dim(nrow=, ncol=, ndim=)</td></tr><tr><td>New array (strings</td><td>arrayname &#x3C;- array(data, dim(nrow=, ncol=, ndim=)</td></tr></tbody></table>

### Array Elements

<table><thead><tr><th width="303">Action</th><th>Syntax</th></tr></thead><tbody><tr><td>Access an array element</td><td>array[row position, column position, dimension]</td></tr><tr><td>Check if an item exists</td><td>value %in% array</td></tr><tr><td>Sort array increasing</td><td>sort(array)</td></tr><tr><td>Sort array decreasing</td><td>sort(array, decreasing = TRUE)</td></tr></tbody></table>

#### Inputs:

```r
#Creating array
a <- array(c(1:20),dim = c(4,4,2))

#Access element at row=4, column=4, dimension=1
a[4, 4, 1]

#Check if item exists in array
2 %in% a

#Sort increasing
b <- array(c(16:1),dim = c(4,4,1))
sort(b)

#Sort decreasing
c <- array(c(1:16),dim = c(4,4,1))
sort(c, decreasing = TRUE)
```

#### Outputs:

```r
#Access element at row=4, column=4, dimension=1
16

#Check if item exists in array
TRUE

#Sort increasing
1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16

#Sort decreasing
16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1
```

## Resources

* R Documentation: [Lists](https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Lists)
* R Documentation: [Matrices](https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Index-matrices)
* R Documentation: [Arrays](https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Arrays)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.bcbi.brown.edu/codiac-for-health/computing/r/collections-and-data-structures.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
