Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label R Lists. Show all posts
Showing posts with label R Lists. Show all posts

Tuesday 11 September 2018

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

 A List is a set of values that can have different basic data types, In R List is created using the 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

  1. #Declare a numeric vector
  2. numeric_vector<-c(10,20,30)
  3. #Create list
  4. test_list<- list("Ashish Awasthi", numeric_vector, 12.5,10)
  5. #Print list
  6. print(test_list)

and output is

  1. [[1]]
  2. [1] "Ashish Awasthi"
  3. [[2]]
  4. [1] 10 20 30
  5. [[3]]
  6. [1] 12.5
  7. [[4]]
  8. [1] 10

Access List elements in R

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

  1. #Declare a numeric vector
  2. numeric_vector<-c(10,20,30)
  3. #Create list
  4. test_list<- list("Ashish Awasthi", numeric_vector, 12.5,10,matrix(c(1,2,3,4,5,6), nrow = 2))
  5. #Access List elements using index
  6. print(test_list[1])
  7. print(test_list[2])
  8. print(test_list[4])
  9. print(test_list[5])

and output on R console is


Name List elements in R

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

  1. #Create list
  2. test_list<- list("Ashish Awasthi", c(10,20,30),10,matrix(c(1,2,3,4,5,6), nrow = 2))
  3. #Name list elements
  4. names(test_list) <- c("Name","Vector","Integer","Matrix")
  5. #Print Named List
  6. print(test_list)

and output is

  1. $`Name`
  2. [1] "Ashish Awasthi"
  3. $Vector
  4. [1] 10 20 30
  5. $Integer
  6. [1] 10
  7. $Matrix
  8. [,1] [,2] [,3]
  9. [1,] 1 3 5
  10. [2,] 2 4 6

Access list elements using names

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

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

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.

  1. #Create list
  2. test_list<- list("Ashish Awasthi", c(10,20,30),10,matrix(c(1,2,3,4,5,6), nrow = 2))
  3. #Print List
  4. print(test_list)
  5. #Update first element of the list
  6. test_list[1] <- "R Programming"
  7. #Add a new element at the end of the list
  8. test_list[5] <- 1000
  9. #Delete the third element
  10. test_list[3] <- NULL
  11. #Print List
  12. print(test_list)

See the output - the first print statement prints the original list and the second one prints the updated list

  1. #Original List Data
  2. [[1]]
  3. [1] "Ashish Awasthi"
  4. [[2]]
  5. [1] 10 20 30
  6. [[3]]
  7. [1] 10
  8. [[4]]
  9. [,1] [,2] [,3]
  10. [1,] 1 3 5
  11. [2,] 2 4 6
  12. #Updated List Data
  13. [[1]]
  14. [1] "R Programming"
  15. [[2]]
  16. [1] 10 20 30
  17. [[3]]
  18. [,1] [,2] [,3]
  19. [1,] 1 3 5
  20. [2,] 2 4 6
  21. [[4]]
  22. [1] 1000

Merge Lists in R

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

  1. #Create list
  2. list1 <- list(1,2,3,4,5)
  3. list2 <- list("Jan","Feb","Mar","Apr","May")
  4. #Merging both lists
  5. list3 <- c(list1,list2)
  6. print(list3)

and output is

  1. [[1]]
  2. [1] 1
  3. [[2]]
  4. [1] 2
  5. [[3]]
  6. [1] 3
  7. [[4]]
  8. [1] 4
  9. [[5]]
  10. [1] 5
  11. [[6]]
  12. [1] "Jan"
  13. [[7]]
  14. [1] "Feb"
  15. [[8]]
  16. [1] "Mar"
  17. [[9]]
  18. [1] "Apr"
  19. [[10]]
  20. [1] "May"

Convert a List to Vector

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

  1. #Create list
  2. list1 <- list(1,2,3,4,5)
  3. #Convert list to vector
  4. v <- unlist(list1)
  5. print(v)

and output is

  1. [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.

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

Cheers 🙂 Happy Learning