w3resource

R Programming - Assign NULL to a given list elements

R Programming: List Exercise-14 with Solution

Write a R program to assign NULL to a given list element.

Sample Solution :

R Programming Code :

# Create a list with five elements
l = list(1, 2, 3, 4, 5)

# Print the original list
print("Original list:")
print(l)

# Print message indicating the modification
print("Set 2nd and 3rd elements to NULL")

# Set the 2nd element of the list to NULL
l[2] = list(NULL)

# Set the 3rd element of the list to NULL
l[3] = list(NULL)

# Print the modified list
print(l)

Output:

[1] "Original list:"
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[1] "Set 2nd and 3rd elements to NULL"
[[1]]
[1] 1

[[2]]
NULL

[[3]]
NULL

[[4]]
[1] 4

[[5]]
[1] 5                         

Explanation:

  • Create a list: l = list(1, 2, 3, 4, 5)
    • This line creates a list l containing five elements: 1, 2, 3, 4, and 5.
  • Print the original list: print("Original list:")
    • Prints the label "Original list:" to indicate that the following output is the original list.
  • Display the original list: print(l)
    • Displays the contents of the list l before any modifications.
  • Print modification message: print("Set 2nd and 3rd elements to NULL")
    • Prints the label "Set 2nd and 3rd elements to NULL" to indicate that the following changes are being made to the list.
  • Set 2nd element to NULL: l[2] = list(NULL)
    • Sets the 2nd element of the list l to NULL.
  • Set 3rd element to NULL: l[3] = list(NULL)
    • Sets the 3rd element of the list l to NULL.
  • Print the modified list: print(l)
    • Displays the contents of the list l after the modifications, showing that the 2nd and 3rd elements have been replaced with NULL.

R Programming Code Editor:



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

Previous: Write a R program to convert a given matrix to a list.
Next: Write a R program to create a list named s containing sequence of 15 capital letters, starting from ‘E’.

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-14.php