w3resource

R Programming: List with a Vector, Matrix, and Named Elements

R Programming: List Exercise-2 with Solution

Write a R program to list containing a vector, a matrix and a list and give names to the elements in the list.

Sample Solution :

R Programming Code :

# Create a list containing a vector, a matrix, and another list
list_data <- list(c("Red", "Green", "Black"),     # Vector of color names
                  matrix(c(1,3,5,7,9,11), nrow = 2),  # 2x3 matrix of odd numbers
                  list("Python", "PHP", "Java"))      # List of programming languages

# Print the original list
print("List:")
print(list_data)

# Assign names to the elements in the list
names(list_data) = c("Color", "Odd numbers", "Language(s)")

# Print the list with the assigned column names
print("List with column names:")
print(list_data)

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"                         

Explanation:

  • list_data <- list(c("Red","Green","Black"), matrix(c(1,3,5,7,9,11), nrow = 2), list("Python", "PHP", "Java")):
    • Creates a list called list_data containing three elements:
      • A vector with three color names: "Red", "Green", "Black".
      • A 2x3 matrix of odd numbers: 1, 3, 5, 7, 9, 11.
      • A list of programming languages: "Python", "PHP", "Java".
  • print("List:"):
    • Prints the label "List:".
  • print(list_data):
    • Prints the contents of the list_data list, displaying the vector, matrix, and list.
  • names(list_data) = c("Color", "Odd numbers", "Language(s)"):
    • Assigns names to the elements of the list_data list:
      • "Color" for the color vector.
      • "Odd numbers" for the matrix of odd numbers.
      • "Language(s)" for the list of programming languages.
    print("List with column names:"):
    Prints the label "List with column names:".
    print(list_data):
    Prints the list with the newly assigned element names for better readability.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to create a list containing strings, numbers, vectors and a logical values.
Next: 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.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/r-programming-exercises/list/r-programming-list-exercise-2.php