Copyright

Lists in R Programming: Purpose & Examples

Instructor: Alexis Kypridemos

Alexis is a technical writer for an IT company and has worked in publishing as a writer, editor and web designer. He has a BA in Communication.

A list is a fundamental data structure in R programming and in this lesson, we will talk more about lists and about the different types of operations that one can perform on lists.

Data Structures in R Programming

R programming contains several data structures for grouping data. Those of you who are familiar with other programming languages, will recognize R's vectors to be one-dimensional arrays. Vectors are used to contain multiple items of the same data type.

In simpler terms, vectors can be used to store multiple numbers, or multiple character items (like words), and so on.

The following are examples of vectors:

numericVector <- c(1, 3, 5, 7, 9)
characterVector <- c('one', 'three', 'five', 'seven', 'nine')

Note how the first vector, 'numericVector', contains only numbers, and the second vector, 'characterVector' contains only words.

While vectors can be used to store items of different data types, like both words and numbers, this is generally to be avoided, as more than likely the vector will convert all the items to the character data type.

Data frame is another type of data structure in R. Just as vectors are one-dimensional arrays, data frames are two-dimensional arrays. In simple terms, data frames are similar to spreadsheets, they contain data in columns and rows.

One characteristic of data frames is that each of its columns can contain a different data type.

In the following example, the two vectors defined above, are copied into the data frame named 'exampleDataFrame':

exampleDataFrame <-data.frame(numericVector, characterVector)

In the resulting data frame, 'numericVector' will be represented as a column holding only numbers and 'characterVector' as a column of only character values, i.e. words.

A limitation of data frames is that the columns they contain cannot hold values of more than one data type. So a data frame column cannot contain numbers and words, for example.

Lists and How to Create Them

And this is where the list data structure comes in: lists can store multiple items of any kind of data type.

The following example illustrates creating a list named 'myList', and how it can contain single numeric values (numbers), single character values (words), vectors and even a data frame:

myList <- list(1, 2, 3, 'one', 'two', 'three', numericVector, characterVector, exampleDataFrame)

To print the contents of the list, enter either myList or print(myList) and run the command.

Each of the contents will appear on a new line. Before each content will be its index number in double square brackets, as in [[1]], [[2]], etc.

Index numbers simply represent the order in which each item is stored in the list. Index numbering begins at 1, not 0.

Working with Lists

As demonstrated above, lists can hold all kinds of data.

To view the data type of each item stored in a list, use the 'structure' or 'summary' functions, str() and summary(), like below:

str(myList)
summary(myList)

Running the above commands will return the contents stored in the list, as well as stating the data type of each item, for example, character, numeric, data frame, etc.

If a user encounters an element in R code that they have not created, and want to check if that element is a list, they can use the typeof() or class() functions.

typeof(myList)
class(myList)

If the element is in fact a list, these functions will return 'list'.

Similarly, to view how many elements are stored in a list, the length() function can be used:

length(myList)

To work with individual list items, we can use their index number in square brackets after the list name, like below:

myList[5]

Running the above should return the value 'two', the fifth item in the list.

So what if we want to change a single value in a list? Using the above syntax, we can assign a different value to that item, using the assignment operator, <- :

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support