If
a <- c(6,4,5,6,2,6,0,0,1,3,7,0,0) # index is 11
I need the column index of 7.
Purpose:
I need to reassign value to the last non-zero element, which is the 7 in the above case.
It's guaranteed that the vector a always ends with N consecutive zeros, where N unknown.
You can try
tail(which(a!=0),1)
#[1] 11
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With