For a vector, let's say v = 1:10you can remove elements from vusing negative indexing, for example. v[-1], v[-length(v)], v[-c(2,3)]To remove the first, the last and second / third elements respectively.
I would like to split vby going to the split index n, taking values from 0 to length(v). Code below:
v1 <- v[1:n]
v2 <- v[-c(1:n)]
except works fine n = 0. Now I know that it is 1:nusually unsafe and should be replaced with seq_len(n), however, assignment v2 <- v[-seq_len(0)]creates an empty vector.
Is there a way to make this “safe” using parenthesis notation? Otherwise, I know how to do this using head and tails:
v1 <- head(v, n)
v2 <- tail(v, length(v) - n)
Corresponding to another q / as:
Addition to an empty index vector - empty pointer vector
source
share