I am trying to get a particular diagonal from my data. This is my code :
a2008=c(30,50,65)
a2009=c(40,90,NA)
a2010=c(55,NA,NA)
mydata=rbind(a2008,a2009,a2010)
mydata
[,1] [,2] [,3]
a2008 30 50 65
a2009 40 90 NA
a2010 55 NA NA
how can we get the diagonal 65, 90, 55 ?
I already used the diag
function but i got 30, 90, NA
.
Some help would be appreciated
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With