R Lists – Create, Name, Access, Update elements of list in R

Sharing is Caring

A List is a set of values that can have the different basic data type, In R List is created using list() function. A list can contain a vector or matrix as an element.

Creating a List in R

See this example of creating a list that has vector, integer and characters as its elements

#Declare a numeric vector
numeric_vector<-c(10,20,30)

#Create list 
test_list<- list("Ashish Awasthi", numeric_vector, 12.5,10)

#Print list
print(test_list)

and output is

[[1]]
[1] "Ashish Awasthi"

[[2]]
[1] 10 20 30

[[3]]
[1] 12.5

[[4]]
[1] 10

Access List elements in R

Here I am taking a list that has Vector, Character, Integer and a Matrix as it’s elements and we can access list elements using index (its same as the vector)

#Declare a numeric vector
numeric_vector<-c(10,20,30)

#Create list 
test_list<- list("Ashish Awasthi", numeric_vector, 12.5,10,matrix(c(1,2,3,4,5,6), nrow = 2))

#Access List elements using index
print(test_list[1])
print(test_list[2])
print(test_list[4])
print(test_list[5])

and output on R console is

Access Elements using index from R Lists

Name List elements in R

We can assign different names to list elements and then access elements using names, See an example here

#Create list 
test_list<- list("Ashish Awasthi", c(10,20,30),10,matrix(c(1,2,3,4,5,6), nrow = 2))

#Name list elements
names(test_list) <- c("Name","Vector","Integer","Matrix")

#Print Named List
print(test_list)

and output is

$`Name`
[1] "Ashish Awasthi"

$Vector
[1] 10 20 30

$Integer
[1] 10

$Matrix
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Access list elements using names

See this example to understand that how can we access list elements using names, Suppose we want to get the element with name $Vector

> print(test_list$Vector)
[1] 10 20 30
> print(test_list$Name)
[1] "Ashish Awasthi"
> print(test_list$Matrix)
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
>

Update List element in R

We can update any element of the list using the index but can add or remove value only at the end of the list.

#Create list 
test_list<- list("Ashish Awasthi", c(10,20,30),10,matrix(c(1,2,3,4,5,6), nrow = 2))

#Print List
print(test_list)

#Update first element of the list
test_list[1] <- "R Programming"

#Add a new element at the end of the list
test_list[5] <- 1000

#Delete the third element
test_list[3] <- NULL

#Print List
print(test_list)

See the output – first print statement prints the original list and second one prints the updates list

#Original List Data
[[1]]
[1] "Ashish Awasthi"

[[2]]
[1] 10 20 30

[[3]]
[1] 10

[[4]]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6


#Updated List Data
[[1]]
[1] "R Programming"

[[2]]
[1] 10 20 30

[[3]]
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

[[4]]
[1] 1000

Merge Lists in R

We can merge any number lists to one using c() function or list() function. See this example

#Create list 
list1 <- list(1,2,3,4,5)
list2 <- list("Jan","Feb","Mar","Apr","May")

#Merging both lists
list3 <- c(list1,list2)
print(list3)

and output is

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[[6]]
[1] "Jan"

[[7]]
[1] "Feb"

[[8]]
[1] "Mar"

[[9]]
[1] "Apr"

[[10]]
[1] "May"

Convert a List to Vector

We can convert a list to vector using unlist() function.

#Create list 
list1 <- list(1,2,3,4,5)

#Convert list to vector
v <- unlist(list1)
print(v)

and output is

[1] 1 2 3 4 5

Predefined Lists in R

In R, Months and Letters lists are already defined, you can access it using this code.

letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"
[23] "w" "x" "y" "z"
> LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V"
[23] "W" "X" "Y" "Z"
> month.abb
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> month.name
 [1] "January"   "February"  "March"     "April"     "May"       "June"      "July"     
 [8] "August"    "September" "October"   "November"  "December" 
>

Cheers 🙂 Happy Learning

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

Leave a Reply

Your email address will not be published. Required fields are marked *