Collections and Data Structures

Lists

Lists in R are ordered collections of data that can be of different classes.

Creating Lists

ActionSyntax

New list (empty)

listname <- list()

New list (misc)

listname <- list(1L, "abc", 10.3)

Accessing List Elements

ActionSyntax

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

ActionSyntax

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:

#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:

#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

ActionSyntax

New matrix (empty)

matrixname <- matrix()

New matrix (numbers)

matrixname <- matrix(data, nrow=, ncol=)

New matrix (strings)

matrixname <- matrix(data, nrow=, ncol=)

Accessing Matrix Elements

ActionSyntax

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

ActionSyntax

Create an additional row

rbind(matrix, values for new row)

Create an additional column

cbind(matrix, values for new column)

Inputs:

#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:

#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

ActionSyntax

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

ActionSyntax

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:

#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:

#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

Last updated