R Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

R Lists

R Lists

shape Description

List is a heterogeneous data structure. It means that user can put items of different classes or types, in a single list. And moreover, lists are one-dimensional in nature, so all elements of the list will be arrange in one dimension. A data structure is nothing but a one format to maintain the data and store the data, the general format of data structure types are tables, arrays, files, tree, records. The main functionality of data structure is to work on different algorithms. The list() is the function to create the list type.

shape Example

The following is the way to create the list by using set of strings, numeric values, vectors. [c]# Create a list containing strings, numbers, vectors and a logical values. list_data <- list("SPLESSONS", "TUTORIAL", c(21,32,11), TRUE, 51.23, 115.1) print(list_data)[/c] Output: Now compile the code result will be as follows. [c][[1]] [1] "SPLESSONS" [[2]] [1] "TUTORIAL" [[3]] [1] 21 32 11 [[4]] [1] TRUE [[5]] [1] 51.23 [[6]] [1] 115.1 [/c]

Naming To List Elements

The following is an example to give the names to list elements. [c]# Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(9,3,1,5,8,-2), nrow = 2), list("yellow",11.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Show the list. print(list_data)[/c] Output: Now compile the code result will be as follows. [c]$`1st Quarter` [1] "Jan" "Feb" "Mar" $A_Matrix [,1] [,2] [,3] [1,] 9 1 8 [2,] 3 5 -2 $`A Inner list` $`A Inner list`[[1]] [1] "yellow" $`A Inner list`[[2]] [1] 11.3 [/c]

Merging Multiple Lists

The following is an example to merger multiple lists as a single list by using merged.list . [c] # Create two lists. list1 <- list(1,5,12) list2 <- list("Jan","May","Dec") # Merge the two lists. merged.list <- c(list1,list2) # Print the merged list. print(merged.list) [/c] Output: Now compile the code result will be as follows. [c] [[1]] [1] 1 [[2]] [1] 5 [[3]] [1] 12 [[4]] [1] "Jan" [[5]] [1] "May" [[6]] [1] "Dec" [/c]

Conversion Of List To Vector

Vectors are also commonly known as Atomic vectors. atomic vectors are homogeneous data structures. Means in atomic vector, each element should be an object of same class only. So, a character vector, will have all elements of character type, while a numeric vector will have all elements of numeric type. The Unlist() is the function to convert. The following is an example. [c]# Create lists. list1 <- list(5:8) print(list1) list2 <-list(10:14) print(list2) # Convert the lists to vectors. v1 <- unlist(list1) v2 <- unlist(list2) print(v1) print(v2) # Now add the vectors result <- v1+v2 print(result)[/c] Output: Now compile the code result will be as follows. [c]> print(v1) [1] 5 6 7 8 > print(v2) [1] 10 11 12 13 14 > > # Now add the vectors > result <- v1+v2 Warning message: In v1 + v2 : longer object length is not a multiple of shorter object length > print(result) [1] 15 17 19 21 19[/c]

Summary

shape Key Points

  • The list() is the function to create the list type elements.
  • The unlist() is the function to convert to list to other type.
  • The merged.list() is used to merge the all lists as a single unit.