extracting a particular diagonal form a data frame [duplicate] extracting a particular diagonal form a data frame [duplicate] database database

extracting a particular diagonal form a data frame [duplicate]


To get the main anti-diagonal, you can do this. (rev to get stated order)

mydata[row(mydata) + col(mydata) == ncol(mydata) + 1]

You can get the diagonal below that with

mydata[row(mydata) + col(mydata) == ncol(mydata) + 2]

and so on


you can try to get the antidiagonal by

diag(mydata[,nrow(mydata):1])[1] 65 90 55

or

diag(as.matrix(rev(as.data.frame(mydata)))) 


You can get that diagonal by flipping your matrix before calling diag:

diag(mydata[ , 3:1]) # [1] 65 90 55

Doing mydata[ , 3:1] is indexing the columns of your matrix in reverse order.