I have a very specific problem, where I need to use the length of some shorter array, to subset a longer array. I have presented a toy example below. I do not understand why it does not work to just add 1 when indexing, and I don't understand why it returns the long array filled with NA.
x <- letters[1:5]
x
# [1] "a" "b" "c" "d" "e"
y <- letters[1:10]
y
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
y[length(x): length(y)]
# [1] "e" "f" "g" "h" "i" "j"
y[length(x) + 1: length(y)]
# [1] "f" "g" "h" "i" "j" NA NA NA NA NA
y[(length(x) + 1): length(y)]
# [1] "f" "g" "h" "i" "j"
Using y[length(x): length(y)]
almost solves my probem, but the resulting array is too long, I dont want to return 'e', I have to start from one more index to the right. I thought I could solve this by using y[length(x) + 1: length(y)]
, but that gives me, for some reasons, a vector of the same length as y, and fills NA in the end. I found that using (
solved the problem, but again, I don't understand why, and what is happening when I don't use (
, if someone could help me?