Booom...........

LabNotes and Random Thoughts...............

Sunday, July 19, 2015

last 2 row from the last 2 columns

How to get the last two rows of the last two columns in the iris data set ? I saw this question some where in a R basic tutorial.

iris[(nrow(iris)-1): nrow(iris),(ncol(iris)-1):(ncol(iris))]

or a bit simplified version is

nR = nrow(iris)
nC = ncol(iris)
iris[nR-1): nR, nC-1):nC)]
  Petal.Width   Species
1         2.3 virginica

2         1.8 virginica

but its still hard. 
dplyr verison

tail(select(iris, Petal.Width, Species),2)
  Petal.Width   Species
1         2.3 virginica
2         1.8 virginica

No comments: