如何访问R中向量的最后一个值?

这可以通过使用tail函数来完成。

示例

> x<-c(253,254,36,874,351,651,245,274,19,1095)
> tail(x,n=1)
[1] 1095
> data <- data.frame(Class=c("Highschool","Highschool","Graduate","Graduate",
"Graduate","Graduate","Masters","Masters","Masters","PhD"))
> tail(data,n=1)
Class
10 PhD
df = data.frame(matrix(rnorm(20), nrow=5))
> tail(df,n=1)
X1 X2 X3 X4
5 -0.3595053 0.9943738 0.959761 -0.6565688
> tail(df$X4,n=1)
[1] -0.6565688