To copy a vector in R, you can use the assignment operator or the `copy` function.
In this example,
original_vector
with some values.original_vector
to copied_vector
using the assignment operator (<-
).original_vector
will not affect copied_vector
since they are independent copies.# Using assignment operator
original_vector <- c(1, 2, 3, 4, 5)
copied_vector <- original_vector
# original_vector: [1] 1 2 3 4 5 # copied_vector: [1] 1 2 3 4 5
In this example,
original_vector
with some values.copy
function to create an independent copy of original_vector
named copied_vector
.original_vector
will not affect copied_vector
.# Using `copy` function
copy_of_original <- c(1, 2, 3, 4, 5)
copied_vector <- copy(copy_of_original)
# copy_of_original: [1] 1 2 3 4 5 # copied_vector: [1] 1 2 3 4 5
In this tutorial, we learned How to Copy a Vector in R language with well detailed examples.