Collections and Data Structures
Lists
Lists in R are ordered collections of data that can be of different classes.
Creating Lists
Action | Syntax |
---|---|
New list (empty) | listname <- list() |
New list (misc) | listname <- list(1L, "abc", 10.3) |
Accessing List Elements
Action | Syntax |
---|---|
Access an element | list[position] |
Change a value | list[position] <- newvalue |
See number of values in a list | length(list) |
See if item is present in a list | item %in% list |
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:
Outputs:
Matrices
Creating Matrices
Action | Syntax |
---|---|
New matrix (empty) | matrixname <- matrix() |
New matrix (numbers) | matrixname <- matrix(data, nrow=, ncol=) |
New matrix (strings) | matrixname <- matrix(data, nrow=, ncol=) |
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:
Outputs:
Arrays
Creating Arrays
Action | Syntax |
---|---|
New array (empty) | arrayname <- array() |
New array (numbers) | arrayname <- array(data, dim(nrow=, ncol=, ndim=) |
New array (strings | arrayname <- array(data, dim(nrow=, ncol=, ndim=) |
Array Elements
Action | Syntax |
---|---|
Access an array element | array[row position, column position, dimension] |
Check if an item exists | value %in% array |
Sort array increasing | sort(array) |
Sort array decreasing | sort(array, decreasing = TRUE) |
Inputs:
Outputs:
Resources
Last updated