r lists

R Lists

Posted on

R List is a vector that can store elements of different types.

R List Tutorials


  • R – Create List
  • R – Create Empty List in Specific Length
  • R – Name Elements of List
  • R – Access Elements of List
  • R – List Length
  • R – Append Item to List
  • R – Loop through Items in List
  • R – Reverse List
  • R – Check if List is Empty
  • R – Check Item present in List
  • R – Join Lists
  • R – Convert List to Vector
  • R – Convert List to Data Frame

Create List

We call list() function and pass arguments to create a list.

Syntax: list()

Example 1 – create an empty list:

#pass no argument 
myList <- list()
print(myList)

#output:
list()

Example 2 – create a list

mylist <- list("one", "two", "three", "four")
print(mylist)

#output:
[[1]]
[1] "one"

[[2]]
[1] "two"

[[3]]
[1] "three"

[[4]]
[1] "four"

Create an empty list with specific length

Syntax: mylist <- vector("list", n)

Example:

m <- vector("list", 3)
print(typeof(m))
print(length(m))

#output:
[1] "list"
[1] 3

Get List Length

Syntax: length(mylist)

Example:

m <- list(100, TRUE, "one hundred")
listLength = length(m)
cat("List length is :", listLength)

#output:
List length is : 3

Name Elements of List

Syntax: names(mylist) <- c(name1, name2, ...)

Example:

m <- list(100, TRUE, "one hundred")
names(m) <- c("value", "Validity", "Count")
print(m)

#output:
$value
[1] 100

$Validity
[1] TRUE

$Count
[1] "one hundred"

Access Elements of List

Example:

m <- list(100, TRUE, "one hundred")
print(m[1])
print(m[2])
print(m[3])

#output:
[[1]]
[1] 100

[[1]]
[1] TRUE

[[1]]
[1] "one hundred"

Append Item to List

Syntax: append(mylist, item, after = index)

Example 1 – with no argument:

mylist <- list("one", "two")
mylist <- append(mylist, "three")
print(mylist)

#output:
[[1]]
[1] "one"

[[2]]
[1] "two"

[[3]]
[1] "three"

Example 2 – with argument:

mylist <- list("one", "two")
mylist <- append(mylist, "three", after = 1)
print(mylist)

#output:
[[1]]
[1] "one"

[[2]]
[1] "three"

[[3]]
[1] "two"

Loop items in List

Syntax: for (item inlist) { //code }

Example:

m <- list(100, TRUE, "one hundred")
for (item in m) {
  print(item)
}
#output:
[1] 100
[1] TRUE
[1] "one hundred"

Reverse a List

Syntax: rev(mylist)

Example:

m <- list(100, TRUE, "one hundred")
m1 <- rev(m)
print(m1)

#output:
[[1]]
[1] "one hundred"

[[2]]
[1] TRUE

[[3]]
[1] 100

Check if List is Empty

Syntax: length(mylist) == 0

Example:

m <- list()

if (length(m) == 0) {
  print("List is empty.")
} else {
  print("List is not empty.")
}

#output:
[1] "List is empty."

Check if Item is Present in List

Syntax: item %in% mylist

Example:

m <- list(100, TRUE, "one hundred")
item <- "one hundred"

if (item %in% m) {
  print("Item is present in the List")
} else {
  print("Item is not present in the List")
}
#output:
[1] "Item is present in the List"

Join Lists in R

Syntax: c(list1, list2, list3)

Example:

m1 <- list(1, 2, 3)
m2 <- list("one", "two")

joinList <- c(m1, m2)
print(joinList)

#output:
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] "one"

[[5]]
[1] "two"

Convert List to Vector in R

Syntax: unlist(mylist)

Example:

m1 <- list(1, 2, 3)
m2 <- unlist(m1)
print(m2)

#output:
[1] 1 2 3

Convert List to DataFrame in R

Syntax: as.data.frame(mylist)

Example 1 – list contains no named vectors:

my_list <- list(5, 6, 7)
df <- as.data.frame(my_list)
print(df)

#output:
  X5 X6 X7
1  5  6  7

Example 2 – list contains named vectors:

list_1 <- list(a = c(11, 12, 13), b = c(24, 25, 26))
df <- as.data.frame(list_1)
print(df)

#output:
   a  b
1 11 24
2 12 25
3 13 26

Example 3 – list contains named vectors (another method):

list_1 <- list(c(11, 12, 13), c(24, 25, 26))
df <- as.data.frame(list_1, col.names = c("a", "b"))
print(df)

#output:
   a  b
1 11 24
2 12 25
3 13 26

Conclusion:

We have learned about list in R programming. Hope you understand it well.