In R, I'm imported a data frame of 2,321,123 by 4 called "dataF". I converted the data frame "dataF" to a matrix dataM <- as.matrix(dataF) Does R have an efficient routine to treat the special elements that contain "inf" in them. For example, can you separate the rows that have "inf" elements from the matrix into a separate matrix without iterating over the entire matrix? Also, does R have an efficient way to sort columns in a matrix? -Melanie
Melanie Vida <mvida <at> mac.com> writes: : : In R, I'm imported a data frame of 2,321,123 by 4 called "dataF". : I converted the data frame "dataF" to a matrix : : dataM <- as.matrix(dataF) : : Does R have an efficient routine to treat the special elements that : contain "inf" in them. For example, can you separate the rows that have : "inf" elements from the matrix into a separate matrix without iterating : over the entire matrix? This will eliminate all rows that contain Inf or -Inf. m[apply(is.finite(m), 1, all),] : : Also, does R have an efficient way to sort columns in a matrix? This can be interpreted a number of different ways: See ?order ?sort m[order(m[,1]),] # sorts so that column 1 is sorted and rows stay together apply(m, 2, sort) # sort each column separately
On Sun, 20 Feb 2005, Melanie Vida wrote:> In R, I'm imported a data frame of 2,321,123 by 4 called "dataF". > I converted the data frame "dataF" to a matrix > > dataM <- as.matrix(dataF)I am assuming this is a numeric matrix, and "inf" means Inf and in places that there are no NAs.> Does R have an efficient routine to treat the special elements that contain > "inf" in them. For example, can you separate the rows that have "inf" > elements from the matrix into a separate matrix without iterating over the > entire matrix?Not really, but is.infinite will do a C-level iteration, as will = dataM[rowSums(is.infinite(dataM)) > 0, ] # rows containing Inf or -Inf dataM[rowSums(dataM == Inf), ] # rows containing Inf (no NAs) A <- !is.na(dataM) & dataM == Inf dataM[rowSums(A), ] # rows containing Inf (possible NAs)> Also, does R have an efficient way to sort columns in a matrix?How about for(i in 1:4) dataM[, i] <- sort(dataM[, i], method ="quick") ? -- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595