[R] Identifying integers (as opposed to real #s) in matrix
Romain Francois
romain.francois at dbmail.com
Tue Aug 10 10:13:39 CEST 2010
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 Katz
Something 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
More information about the R-help
mailing list