David Katz
2010-Aug-10 08:05 UTC
[R] Identifying integers (as opposed to real #s) in matrix
Is there a way to identify (for subsequent replacement) which rows in a matrix are comprised entirely of *integers*? I have a large set of *nx3 *matrices where each row either consists of a set of 3 integers or a set of 3 real numbers. A given matrix might looks something like this: [ ,1] [ ,2] [ ,3] [1, ] 121.0000 -98.0000 276.0000 [2, ] 10.1234 25.4573 -188.9204 [3, ] 121.0000 -98.0000 276.0000 [4, ] -214.4982 -99.1043 -312.0495 ..... [n, ] 99.0000 1.0000 -222.0000 Ultimately, I'm going to replace the values in the integer-only rows with "NAs." But first I need r to recognize the integer-only rows. I assume whatever function I write will be keyed off of the ".0000s", but have no clue how to write that function. Any ideas? David Katz [[alternative HTML version deleted]]
Romain Francois
2010-Aug-10 08:13 UTC
[R] Identifying integers (as opposed to real #s) in matrix
Le 10/08/10 10:05, David Katz a ?crit :> > Is there a way to identify (for subsequent replacement) which rows in a > matrix are comprised entirely of *integers*? I have a large set of > *nx3 *matrices > where each row either consists of a set of 3 integers or a set of 3 real > numbers. A given matrix might looks something like this: > > [ ,1] [ ,2] [ ,3] > > [1, ] 121.0000 -98.0000 276.0000 > > [2, ] 10.1234 25.4573 -188.9204 > > [3, ] 121.0000 -98.0000 276.0000 > > [4, ] -214.4982 -99.1043 -312.0495 > > ..... > > [n, ] 99.0000 1.0000 -222.0000 > > Ultimately, I'm going to replace the values in the integer-only rows with > "NAs." But first I need r to recognize the integer-only rows. I assume > whatever function I write will be keyed off of the ".0000s", but have no > clue how to write that function. Any ideas? > > David KatzSomething like this perhaps: > x <- rbind( c(1, 2, 3), c(1.2,2.2, 3.2), c(1,2,3), c(1.2, 1.2, 1.3 ) ) > x [,1] [,2] [,3] [1,] 1.0 2.0 3.0 [2,] 1.2 2.2 3.2 [3,] 1.0 2.0 3.0 [4,] 1.2 1.2 1.3 > rowSums( x == round(x) ) == ncol(x) [1] TRUE FALSE TRUE FALSE > x[ rowSums( x == round(x) ) == ncol(x) , ] <- NA > x [,1] [,2] [,3] [1,] NA NA NA [2,] 1.2 2.2 3.2 [3,] NA NA NA [4,] 1.2 1.2 1.3 Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://bit.ly/aAyra4 : highlight 0.2-2 |- http://bit.ly/94EBKx : inline 0.3.6 `- http://bit.ly/aryfrk : useR! 2010
Apparently Analagous Threads
- Rationale behind render opposed to render_action
- Why mp3 (licensing issues) as opposed to Open Source OGG
- indexing tuples (example: "frog" => 123) as opposed to words
- Retrieving sound files from DB as opposed to filesystem
- Active directory, winbind, and distribution groups (as opposed to security groups)