To get a sub-vector in R, you can use indexing or the subset
function.
In this example,
vec
containing elements.sub_vec
.sub_vec
to standard output.vec <- c(1, 2, 3, 4, 5)
sub_vec <- vec[2:4]
print(sub_vec)
[1] 2 3 4
In this example,
vec
containing elements.subset()
function specifying the vector vec
and the condition to filter elements.subset()
function to standard output.vec <- c("apple", "banana", "cherry", "date")
sub_vec <- subset(vec, vec != "banana")
print(sub_vec)
[1] "apple" "cherry" "date"
In this tutorial, we learned How to get Sub Vector in R language with well detailed examples.