R Program to assign NULL to a given list element

How to assign NULL to a given list element

Here we are explaining how to write an R program to assign NULL to a given list element. Here we are using a NULL object for the assignment. it is a reserved word and it is often returned by the expressions or functions whose value is undefined. The syntax of NULL using conditions

NULL as.null(x, …) is.null(x)

– where x is an object to be tested, and dots(…) are to be ignored.

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

Below are the steps used in the R program to assign NULL value to a given list element. In this R program, we directly give the value NULL to the element. First, assign a list to the variable lis. And assign value NULL by specifying the position of value 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:")
## [1] "Original list is:"
print(lis)
## [[1]]
## [1] 10
## 
## [[2]]
## [1] 20
## 
## [[3]]
## [1] 30
## 
## [[4]]
## [1] 40
## 
## [[5]]
## [1] 50
print("Set 2nd and 3rd elements changes to NULL")
## [1] "Set 2nd and 3rd elements changes to NULL"
lis[2] = list(NULL) 
lis[3] = list(NULL) 

print(lis)
## [[1]]
## [1] 10
## 
## [[2]]
## NULL
## 
## [[3]]
## NULL
## 
## [[4]]
## [1] 40
## 
## [[5]]
## [1] 50

R Source Code

lis = list(10, 20, 30, 40, 50, 60, 70, 80, 90)

print("Original list is:")
## [1] "Original list is:"
print(lis)
## [[1]]
## [1] 10
## 
## [[2]]
## [1] 20
## 
## [[3]]
## [1] 30
## 
## [[4]]
## [1] 40
## 
## [[5]]
## [1] 50
## 
## [[6]]
## [1] 60
## 
## [[7]]
## [1] 70
## 
## [[8]]
## [1] 80
## 
## [[9]]
## [1] 90
print("Set 2nd and 3rd elements changes to NULL")
## [1] "Set 2nd and 3rd elements changes to NULL"
lis[3] = list(NULL) 
lis[5] = list(NULL) 
lis[7] = list(NULL) 

print(lis)
## [[1]]
## [1] 10
## 
## [[2]]
## [1] 20
## 
## [[3]]
## NULL
## 
## [[4]]
## [1] 40
## 
## [[5]]
## NULL
## 
## [[6]]
## [1] 60
## 
## [[7]]
## NULL
## 
## [[8]]
## [1] 80
## 
## [[9]]
## [1] 90