R - List

Card Puncher Data Processing

About

The list in R may contain elements of the different class (just like a data frame)

Collection of object which must have the same class of objects are:

data.tables and data.frames are internally lists with all its columns of equal length and with a class attribute.

Syntax

myList = list(object,object,...)  

Example

  • List
> v = list("Nico has",40,"year")
> v
[[1]]
[1] "Nico has"

[[2]]
[1] 40

[[3]]
[1] "year"
  • List of vector (for the dimension of a matrix
dimnames = list(c("Row1", "Row2"), c("Col1", "Col2", "Col3"))
> dimnames
[[1]]
[1] "Row1" "Row2"

[[2]]
[1] "Col1" "Col2" "Col3"

Attributes

Names

names: Functions to get or set the variable name of a value.

l = list(name = "Nico", age = 40)
l
$name
[1] "Nico"

$age
[1] 40

> names(l)
[1] "name" "age" 

  • You can access the value then by name
l$name
[1] "Nico"

How to

subset

Example using the subset operators

> l = list(name = c("Nico","Mad","Mel"), age = c(40,7,4), colour=c("Red","Blue","Parse"))
> l
$name
[1] "Nico" "Mad"  "Mel" 

$age
[1] 40  7  4

$colour
[1] "Red"   "Blue"  "Parse"
  • Indexing by key (return a list)
> l[1]
$name
[1] "Nico" "Mad"  "Mel" 
  • Indexing by Name (return a list)
> l["name"]
$name
[1] "Nico" "Mad"  "Mel" 
  • Indexing by vector (return a list)
> name <- "name"
> l[name]
$name
[1] "Nico" "Mad"  "Mel"
  • Indexing by key and returning a vector
> l[[1]]
[1] "Nico" "Mad"  "Mel" 
  • Indexing by name and returning a vector
> l$name
[1] "Nico" "Mad"  "Mel" 
# of
> l[["name"]]
[1] "Nico" "Mad"  "Mel" 
  • Indexing by vector and returning a vector
> name <- "name"
> l[[name]]
[1] "Nico" "Mad"  "Mel"
  • Retrieving the first and third elements in the list
> l[c(1,3)]
$name
[1] "Nico" "Mad"  "Mel" 

$colour
[1] "Red"   "Blue"  "Parse"
  • Retrieving the third element of the first element
l[[c(1,3)]]
[1] "Mel"
# of
> l[[1]][[3]]
[1] "Mel"
  • Partial Matching when retrieving a vector
> l[["n"]]
NULL
> l[["n", exact=FALSE]]
[1] "Nico" "Mad"  "Mel" 

Apply

lapply apply a function over an list.

Function

Class

> class(v)
[1] "list"

Length

> l = list("Nico has",40,"year")
> length(l)
[1] 3

Structure

Data Structure

> l = list("Nico has",40,"year")
> str(v)
List of 3
 $ : chr "Nico has"
 $ : num 40
 $ : chr "year"





Discover More
Card Puncher Data Processing
R - (Datatype|Type|Storage Mode) of an object (typeof, mode)

In the C code underlying R, all objects are pointers to a structure with typedef SEXPREC; the different R data types are represented in C by SEXPTYPE, which determines how the information in the various...
Card Puncher Data Processing
R - (Object|Variable|Symbol)

R provides a number of specialized objects. They are created (instantiated), used and referenced through variable (known as symbol). When you read the term object in the documentation, you can interchange...
Card Puncher Data Processing
R - Apply a function over a data structure

The apply function call a function over a data structure: apply do it over a array or matrix lapply do it over a list ...
Card Puncher Data Processing
Backlinks

A class is just an attribute of an object. You can remove the attribute with the function . The class with then become the type A list can have different class. unclass returns (a copy of) its...
Card Puncher Data Processing
R - Data Table

A data table is an enhanced data.frame. data.tables (and data.frames) are internally lists as well, but with all its columns of equal length and with a class attribute. data.table function ...
R Studio Import Dataset
R - Data frame Object

A data frame is a logical implementation of a table in a relational database A data frame inherits all the property and function of an object. It has a list of variables of the same number of rows with...
Card Puncher Data Processing
R - Matrix

The matrix object in R is an array of two dimensions with the same class (data type). You can create a matrix with three methods: the matrix method the columns and rows bindings methods the...
Card Puncher Data Processing
R - Names

in R. A name permits to address a value. An object may have several values and then several names. For a data.frame or a list, it will be the column header. You can set and/or get them. The...
Card Puncher Data Processing
R - Subset Operators (Extract or Replace Parts of an Object)

In the Data Manipulation category, you have the subset operators: [ [[ $ This operators acts on: vectors, matrices, arrays lists data.frame to extract or replace parts. See also:...
Card Puncher Data Processing
R - Summary

summary statistics function summary is a generic function used to produce result summaries of the results of various model fitting functions. The function invokes particular methods which depend...



Share this page:
Follow us:
Task Runner