I am new to the R language, so I just want to know how array indexing is done in R. I mean like sorting or any calculati开发者_开发知识库ons that involve 2D arrays.
It depends,
To index elements use square brackets: ar[1]
, or ar[1,1]
for 2d.
Whole columns and rows are: ar[,1]
or ar[1,]
For sorting, look at the sort
and order
functions.
For calculations using 2d arrays, you can have:
Elementwise: ar1+ar2
, ar1*ar2
Inner product: ar1%*%ar2
Outer product: outer(ar1,ar2)
or ar1%o%ar2
You must take care that the dimensions of the arrays are correct for what you want to do, though R will automatically try to recycle elements to complete a calculation.
One thing to note is that indexing is 1-based, not 0-based as in most languages, ie the first element is ar[1]
.
精彩评论