如何在 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

更新于: 06-07-2020

1 千次浏览

启动您的职业生涯

完成课程获得认证

开始
广告