w3resource

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


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?



Follow us on Facebook and Twitter for latest update.