Ch 9-10 Subsetting Ordering Array operations Iteration © Fall 2004 Don Edwards and the University of South Carolina
9 Subsetting Extraction with “[]” for vectors and matrices : and c() Rows, columns, and sub-matrices Indices are not extracted Extraction with logical vectors Statements look self-referential We can recover index information with match() or which() for unique entries.
9 Sorting Order function Sort function lacks functionality Creates vector of indices that can then be used to “extract” ordered data files Like a logical vector, the focus is on the index object, not the output object Sort function lacks functionality Ordering of factor levels may not be alphabetical
10 Matrix Operations Splus was very inefficient at looping R is improved Both Splus and R have built-in iterative capabilities Functions that operate on vectors and arrays The apply() function tapply() works on groups (defined by factors) tapply() is quite a versatile feature
10 Iteration If we need to iterate, we use for() and while() statements for() uses a counter while() uses a logical comparison If for() is slow, R can call both C++ and Fortran subroutines