w3resource

R Programming: Create a list containing strings, numbers, vectors, and logical values

R Programming: List Exercise-1 with Solution

Write a R program to create a list containing strings, numbers, vectors and a logical values.

Sample Solution :

R Programming Code :

# Create a list containing strings, a vector, a logical value, and numbers
list_data = list("Python", "PHP", c(5, 7, 9, 11), TRUE, 125.17, 75.83)

# Print a message to indicate the data of the list
print("Data of the list:")

# Print the actual list data
print(list_data)

Output:

[1] "Data of the list:"
[[1]]
[1] "Python"

[[2]]
[1] "PHP"

[[3]]
[1]  5  7  9 11

[[4]]
[1] TRUE

[[5]]
[1] 125.17

[[6]]
[1] 75.83                         

Explanation:

  • list_data = list("Python", "PHP", c(5, 7, 9, 11), TRUE, 125.17, 75.83):
    • This line creates a list called list_data in R that contains multiple data types:
      • Two string elements: "Python" and "PHP"
      • A numeric vector: c(5, 7, 9, 11)
      • A logical value: TRUE
      • Two numeric elements: 125.17 and 75.83
  • print("Data of the list:"):
    • This line prints the message "Data of the list:" to the console, indicating that the list data will be printed next.
  • print(list_data):
    • This line prints the contents of the list_data list, displaying all the elements it contains. Each element in the list is displayed in its respective position.

R Programming Code Editor:



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

Previous: R Programming Exercises Home.
Next: Write a R program to list containing a vector, a matrix and a list and give names to the elements in 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://www.w3resource.com/r-programming-exercises/list/r-programming-list-exercise-1.php