How to access the last value in a vector?

后端 未结 11 1253
栀梦
栀梦 2020-11-28 18:05

Suppose I have a vector that is nested in a dataframe one or two levels. Is there a quick and dirty way to access the last value, without using the length() fu

11条回答
  •  执念已碎
    2020-11-28 18:18

    If you're looking for something as nice as Python's x[-1] notation, I think you're out of luck. The standard idiom is

    x[length(x)]  
    

    but it's easy enough to write a function to do this:

    last <- function(x) { return( x[length(x)] ) }
    

    This missing feature in R annoys me too!

提交回复
热议问题