I have a pretty simple problem that brings me sadness despite a couple of hours of hair hanging, so I thought I would ask for your help. I am looking for a simple way to return a vector containing only the last element of some source vector.
This is my original vector, 'a':
a<-c(0,0,1,0,0,1,0,0,1,0)
I want to create a vector "b" that has the same length as "a" and carries only the last element, which does not make sense. In other words,
b = (0,0,0,0,0,0,0,0,1,0)
I was able to do this by constructing a loop going back from the end of the 'a' vector to its first element, but this seems clearly inelegant. I am sure there is a better way.
In case someone is interested, a more serious problem: I try to change the value of one vector, where it corresponds to the last non-empty element of another vector.
Thanks so much for any help.