Lists in R Language: Create, Name, and Append

Before understanding and working on lists in R Language, let’s review the other data types in R Language.

Each of the data types (vectors, matrices, and data frames) has some level of constraints. For example, vectors are single-column data types and can only store one type of data. Matrices are of two-dimensional, but they can store only one type of data. On the other hand, data frames are two-dimensional and can store different types of data, but in data frames, the length of columns should be the same.

Lists in R Language

Lists in R are a fundamental data structure used to store collections of elements. Lists offer a flexible way to organize data of different types, unlike vectors that can hold elements of the same data type. Lists in R language have no such constraints as vectors, matrices, and data frames. The element of a list can contain any type of data can contain any type of data having varying lengths for each element.

Lists are the most flexible data type in R, as the list can hold all kinds of different operations when programming. Let us consider some examples of how to create lists in R, and how elements of the list can be named, retrieved, and appended. The list data type is created using the list() keyword. For examples,

mylist <- list('alpha', 'beta', 'gamma', 1:5, TRUE)

Note that lists are printed differently and they have their form of indexing for retrieving each element of the list.

Retrieving List Elements

A certain element of a list can be accessed using the subsetting technique (based on the list indexing mechanism. For example, to access the first three elements of the list created above, one can write in R console as,

A certain element of a list can be accessed using a subsetting technique (based on the list indexing mechanism. For example, to access the first three elements of the list created above, one can write in R console as,

mylist[1:3]

To examine one element of the list, double square brackets can be used with the required index number. For example, to access the fourth element of the list, one can use,

mylist[[4]]

Note when one uses normal subsetting, a list will be obtained as a result. When double square brackets are used the contents of the elements are obtained. Note the difference and command and output of each command used below:

mylist[4]
mylist[[4]]

Naming Elements of a List

The elements of a list can be named, and elements of lists can be retrieved using the $ operator instead of square brackets. The first command will name the elements of the mylist object. and then other commands will result in the same output.

names(mylist) <- c("a", "b", "c", "d", "e")

mylist$d
myslit[[4]]
Lists in R Language: Create, Name, and Append

Appending an Element to List

Like data frames, an element of a list can be created by assigning something to an index that does not exist yet. For example, a sixth element is added to the list

mylist$f <- c("Pass", "Fail")

Now the 6th element of the list contains a vector of strings “Pass”, and “Fail”. To check this,

mylist$f
mylist[[6]]

To access the elements of specific list vector subsetting and two square brackets can be used. For example to access the fourth element of the list one can use,

mylist[[4]][3] # 3rd element of 4th list element
mylist[[4]][1:3]# first three elements of 4th list element
mylist$d[3]
  • Remember to name elements for readability when dealing with complex data structures.
  • Lists are versatile for storing various data types, making them a powerful tool for data organization in R.

For further reading about lists see the link Lists in R.

MCQs General Knowledge

Packages in R Programming: An Introduction

The post is an introduction tutorial about Packages in R Programming. In R language functions and datasets are all stored in packages. The content of a package is only available when a package is loaded using the library() function.

To see which R packages are installed, write the following command (without argument)

library( )

To load a particular installed package, use the package name as the argument to the library() function, that is,

library(MASS)

Installing and Updating Packages in R Programming

If the computer system is connected to the internet and a required package is not installed on one’s computer, the user can use the install.packages() function to install the required package. To update the already installed package one can use the update.package() function. The search() function can be used to see which packages are loaded into computer memory.

Classification of R Packages

R packages can be classified as standard (base) packages and contributed packages. The standard (or base) packages are considered part of the R source code. The base packages contain the basic functions that allow R to work. The base packages also contain datasets and standard statistical and graphical functions. The standard R functions are automatically available in any R installation, that is, you do not need to install them.

The standard R packages are written by authors. These packages implement some specialized statistical methods, and access to datasets and hardware. The contributed packages are distributed with every binary distribution of R and are available for download from CRAN and other repositories such as Bioconductor.

Frequently Asked Questions About R: Packages in R Programming

R Namespace

R packages can have a namespace. Namespaces (i) allow the package writer to hide functions and data that are meant only for internal use, (ii) prevent functions from breaking when a user picks a name that clashes with one in the packages, and (iii) provide a way to refer to an object within a particular package.

For example, in R the t() function is the transpose function. A user can define his own t() function. The namespaces will prevent the user’s definition from taking procedure and breaking every function that tries to transpose the matrix.

Two operators work with namespaces, (i) :: double colon operator and triple colon operator :::. The double colon operator selects definitions from a particular namespace. For example, the t() function is available as the base::t, because it is defined in the base package. The function that is exported from the package can be retrieved with a double colon operator.

The tiple colon operator acts as a double colon operator but it also allows access to hidden objects. The getAnywhere() function can be used to search for multiple packages.

Note: Packages are interdependent, and loading one package may cause other packages to be automatically loaded. The colon operators also cause automatic loading of the associated package. the package is not added to the search list when a package with namespaces is loaded automatically.

FAQs about R Packages

  1. What is an R package?
  2. How an R package can be loaded in a session?
  3. What is the use of getAnywhere() function in R?
  4. What is the use of the colon operator for package loading
  5. What is namespace in R language?
  6. Who writes or develops R packages?
Available R Packeges in Local and Global Directory

SPSS Data Analysis

MCQs General Knowledge

Debugging Tools in R Language

The R system has two main ways of reporting a problem in executing a function. One of them is a warning message while the other one is a simple error. The purpose of the warning is to tell the user (programmer) that “something unusual happened during the execution of the function, but the function was nevertheless able to execute to completion”. Writing a robust code (code that checks for imputing errors) is important for larger programs.

log(-1)     #produce a warning (NaN)
message <- function(x){
             if(x > 0)
               print("Hello")
            else
               print("Goodbye")
}

The log(-1) will result in a fatal error, not a warning. The first thing one should do is to print the call stack (print the sequence of function calls that led to the error). The traceback() function can be used which prints the list of functions that were called before the error occurred. However, this can be uninteresting if the error occurred at a top-level function.

Debugging Tools in R

Debugging Tools in R

The debugging tools in R are:

The traceback() Function

The traceback() function prints the sequence of function calls in reverse order from the top.

The debug() Function

The debug() function takes a single argument (the name of a function) and steps through the function line-by-line to identify the specific location of a bug, that function is flagged for debugging. To unflag a function, undebug() function is used. A function flagged for debugging does not execute in a usual way, rather, each statement in the function is executed one at a time and the user can control when each statement gets executed. After a statement is executed, the function suspends and the user is free to interact with the environment.

The browser() Function

The browser() function can be used to suspend the execution of a function so that the user can browse the local environment.

The trace() Function

The trace() function is very useful for making minor modifications to function “on the fly” without having to modify functions and re-sourcing them. It is especially useful if you need to track down an error that occurs in a base function.

trace("mean", quote( if( any(is.nan(x) ) ){ browser() }), print = FALSE)

The trace() function copies the original function code into a temporary location and replaces the original function with a new function containing the insert code.

The recover() Function

The recover() function can help to “jump up” to a higher level in the function call stack.

options(error = recover)

The error option tells R what to do in a situation where a function must halt the execution.

SPSS Data Analysis

r faqs