R Programming: Create and Access elements of a List Containing a Vector, Matrix, and another List
Write a R program to create a list containing a vector, a matrix and a list and give names to the elements in the list. Access the first and second element of the list.
Sample Solution :
R Programming Code :
l# Create a list with a vector, a matrix, and another list
list_data <- list(c("Red","Green","Black"), matrix(c(1,3,5,7,9,11), nrow = 2),
list("Python", "PHP", "Java"))
# Print the original list
print("List:")
print(list_data)
# Assign names to each element of the list
names(list_data) = c("Color", "Odd numbers", "Language(s)")
# Print the list with the assigned names
print("List with column names:")
print(list_data)
# Access and print the first element of the list (the vector)
print('1st element:')
print(list_data[1])
# Access and print the second element of the list (the matrix)
print('2nd element:')
print(list_data[2])
Output:
[1] "List:" [[1]] [1] "Red" "Green" "Black" [[2]] [,1] [,2] [,3] [1,] 1 5 9 [2,] 3 7 11 [[3]] [[3]][[1]] [1] "Python" [[3]][[2]] [1] "PHP" [[3]][[3]] [1] "Java" [1] "List with column names:" $Color [1] "Red" "Green" "Black" $`Odd numbers` [,1] [,2] [,3] [1,] 1 5 9 [2,] 3 7 11 $`Language(s)` $`Language(s)`[[1]] [1] "Python" $`Language(s)`[[2]] [1] "PHP" $`Language(s)`[[3]] [1] "Java" [1] "1st element:" $Color [1] "Red" "Green" "Black" [1] "2nd element:" $`Odd numbers` [,1] [,2] [,3] [1,] 1 5 9 [2,] 3 7 11
Explanation:
- Create a list:
list_data <- list(c("Red","Green","Black"), matrix(c(1,3,5,7,9,11), nrow = 2), list("Python", "PHP", "Java")) - A list list_data is created containing three elements:
- A character vector ("Red", "Green", "Black")
- A 2x3 matrix created from a sequence of odd numbers (1, 3, 5, 7, 9, 11)
- Another list containing programming languages ("Python", "PHP", "Java")
- Print the original list:
print("List:")
print(list_data) - The entire list is printed, showing the contents of each element.
- Assign names to list elements:
names(list_data) = c("Color", "Odd numbers", "Language(s)") - Names are assigned to the three elements of the list:
- "Color" for the first element (vector)
- "Odd numbers" for the second element (matrix)
- "Language(s)" for the third element (list)
- Print the named list:
print("List with column names:")
print(list_data) - The list is printed again, now with the assigned names visible for each element.
- Access and print the first element:
print('1st element:')
print(list_data[1]) - The first element of the list (the vector containing colors) is accessed and printed.
- Access and print the second element:
print('2nd element:')
print(list_data[2]) - The second element of the list (the matrix of odd numbers) is accessed and printed.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to list containing a vector, a matrix and a list and give names to the elements in the list.
Next: Write a R program to create a list containing a vector, a matrix and a list and add element at the end of the list.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics