R Program to assign NULL to a given list element


March 12, 2022, Learn eTutorial
1469

How to assign NULL to a given list element?

Null object or value is a reserved word whose value is zero or Nill, which is usually returned by some functions that don't have any return value. Here we explain how to write an R program to assign NULL to a given list element.

How to assign NULL to a given list element in the R program?

In this R program,  we are assigning the null value to the selected elements in a list. For that, we are adding the list of values to the variable lis. Then we are assigning the null value to the specified position elements in the list.

ALGORITHM

STEP 1: Assign variables lis with a list

STEP 2: Print the original lists

STEP 3:Assign NULL value to 2nd and 3rd element by giving lis[2] = list(NULL) , lis[3] = list(NULL) 

STEP 4: Print the final list

R Source Code

                                          lis = list(10, 20, 30, 40, 50)
print("Original list is:")
print(l)
print("Set 2nd and 3rd elements changes to NULL")
lis[2] = list(NULL) 
lis[3] = list(NULL) 
print(lis)
                                      

OUTPUT

[1] "Original list is:"
[[1]]
[1] 10

[[2]]
[1] 20

[[3]]
[1] 30

[[4]]
[1] 40

[[5]]
[1] 50

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

[[2]]
NULL

[[3]]
NULL

[[4]]
[1] 40

[[5]]
[1] 50