In R a list is a data structure that contains an ordered sequence of objects that can be of different types. Unlike vectors and matrices, lists can contain all sorts of objects including numeric, logical and character vectors or matrices and even other lists.
Construct a list by passing each object you wish the list to hold to the list() function:
In [1]:
# Create a new list containing a vector, a character string and a matrix
new_list <- list( c(1,2,3), "Life is Study", matrix(seq(1,6,1),2,3))
print(new_list)
You can give the objects in a list specific names upon creation:
In [2]:
# Create the same list but give the objects names
new_list <- list(vector= c(1,2,3), # *
char_string = "Life is Study",
my_matrix = matrix(seq(1,6,1),2,3))
print(new_list)
*Note: it is sometimes cleaner to break up long bits of code across multiple lines.
You can access a list's object names or assign new names with the names() function:
In [3]:
names(new_list) # Access names
names(new_list) <- c("obj1","obj2","obj3") # Assign new names
names(new_list) # Access names
Out[3]:
Out[3]:
List Indexing
You can access the contents of lists several different ways. First, you can take slices of a list using the same single square bracket index notation you use with vectors:
In [4]:
new_list[1:2] # Take a slice of the first 2 objects
new_list[c(1,3)] # Take a slice of the first and third objects
new_list[2] # Take a slice that contains only the second object
typeof(new_list[2]) # Check the type of the single-object slice
Out[4]:
Out[4]:
Out[4]:
Out[4]:
Note that list slices returned when you access a list with single brackets give you new, smaller lists, even if the slices only contain one object.
To access a specific member of a list directly, wrap the object's index in double square brackets:
In [5]:
new_list[[2]] # Access the second list object
typeof(new_list[[2]]) # Check the object's type
Out[5]:
Out[5]:
If you gave names to your list objects, you can access them directly using those names either by passing the names as characters wrapped in double square brackets or by using $ followed by the object name:
In [6]:
new_list[["obj3"]] # Access an object directly by name with square brackets
Out[6]:
In [7]:
new_list$obj3 # Access an object directly by name using $
Out[7]:
After directly accessing a list object, you can perform indexing and other operations on the object as usual. For instance, after accessing a matrix in a list you can then index the matrix:
In [8]:
new_list[["obj3"]][2,2] # Get obj3 and then get the value in row 2 column 2
Out[8]:
List Summaries and Alterations
Since lists can hold objects of different types, math operations and many of the other functions that work on vectors and matrices do not work on lists.
The most common list functions are those that give basic information about the list, such as length, str and summary:
In [9]:
length(new_list) # Return the length of a list
str(new_list) # Get an overview of the list's structure
summary(new_list) # Get an summary of the list's contents
Out[9]:
Out[9]:
*Note: In RStudio the environment pane in the upper right corner provides a summary of stored objects. Certain objects like lists have an arrow next to them that you can click on to expand and view additional information about the object's contents.
To add a new object to a list, you can give the list a new name with specified value using the $ notation:
In [10]:
new_list$obj4 <- "Additional object"
str(new_list)
In [11]:
new_list[["obj5"]] <- "The bracket notation also works"
str(new_list)
If you don't want to assign a name, you can add an object using the numeric index notation:
In [12]:
new_list[[6]] <- "this object has no name!"
str(new_list)
The c() function can be used to join two lists together:
In [13]:
second_list <- list("combine me", "with new_list")
combined_list <- c(new_list, second_list)
str(combined_list)
Finally, you can remove list items by assigning them a value of NULL:
In [14]:
combined_list[[8]] <- font=""> NULL # remove item 8
combined_list$obj1 <- font=""> NULL # remove item the with the name obj1
str(combined_list)
->->
In [15]:
combined_list[3:6] <- font=""> NULL # remove items 3 through 6
str(combined_list)
->
In practice, lists are often created as a result of running functions on data so you probably won't spend a lot of time creating them yourself. When you work with data in R, it is more often in the form of a data frames, which are the subject of the next lesson.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.