Q:

Write a R program to add 3 to each element in a given vector. Print the original and new vector

0

Write a R program to add 3 to each element in a given vector. Print the original and new vector.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

v = c(1, 2, NULL, 3, 4, NULL)
print("Original vector:")
print(v)
new_v = (v+3)[(!is.na(v)) & v > 0]
print("New vector:")
print(new_v)

Sample Output:

[1] "Original vector:"
[1] 1 2 3 4
[1] "New vector:"
[1] 4 5 6 7

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now