Refer to the last column in R
just use ncol()
to get the index of the last col
data[,ncol(data)]
To refer to last column:
colnames(data)[ncol(data)]
Take the first element of the rev
ersed vector of column names:
rev(names(mtcars))[1]
[1] "carb"
Similarly, to get the last column, you can use
rev(mtcars)[1]