Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting a particular diagonal form a data frame [duplicate]

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

like image 980
John john Avatar asked Dec 13 '22 17:12

John john


1 Answers

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

like image 106
IceCreamToucan Avatar answered Dec 17 '22 23:12

IceCreamToucan