Here we are explaining how to write an R program to add 3 to each element in a given vector. We can give one vector value to add three to each one. For the calculation of adding here, we use the plus(+) operator. Here is.na() is used for checking whether the element x is present or not if no it will return boolean value TRUE. The syntax of this function is
Given below are the steps which are used in the R program to add 3 to each element. In this R program, we accept the vector values into variables A. The result after adding 3 to each element is stored into a variable new. Finally the variable new is printed as the output vector.
A = c(1, 2, NULL, 3, 4, NULL)
print("Original vector:")
## [1] "Original vector:"
print(A)
## [1] 1 2 3 4
new = (A+3)[(!is.na(A)) & A > 0]
print("New vector:")
## [1] "New vector:"
print(new)
## [1] 4 5 6 7