How to access the last value in a vector in R?

R ProgrammingServer Side ProgrammingProgramming

This can be done by using tail function.

Example

> 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
raja
Published on 06-Jul-2020 14:25:33
Advertisements