w3resource

Creating a List with Character, Numeric, and Logical Vectors in R

R Programming: Basic Exercise-24 with Solution

Write a R program to create a list of heterogeneous data, which include character, numeric and logical vectors. Print the lists.

Sample Solution :

R Programming Code :

# Create a list named 'my_list' with three elements: 
# a character vector ('Chr'), a numeric sequence from 1 to 15 ('nums'), and a logical value ('flag')
my_list = list(Chr="Python", nums = 1:15, flag=TRUE)

# Print the entire list to the console
print(my_list)

Output:

$Chr
[1] "Python"

$nums
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

$flag
[1] TRUE                         

Explanation:

  • my_list = list(Chr="Python", nums = 1:15, flag=TRUE):
    • Creates a list named my_list with three elements:
      • Chr: A character vector with the value "Python".
      • nums: A numeric vector containing integers from 1 to 15.
      • flag: A logical value set to TRUE.
  • print(my_list):
    • Displays the content of my_list, showing each element with its associated name and value.

R Programming Code Editor:



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

Previous: Write a R program to compute sum, mean and product of a given vector elements.
Next: Write a R program to create a Dataframes which contain details of 5 employees and display the details.

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/basic/r-programming-basic-exercise-24.php