[R] Getting wrong NA values using "for" cmd

VictorDelgado victor.maia at fjp.mg.gov.br
Fri Jul 8 17:06:39 CEST 2011


Hi There,

I'm facing one problem to construct a vector using the "for" command: 

I have one matrix named 'dados' (same as /data/ from portuguese), for
example:

> dados[140:150,]
          [,1] [,2] [,3]
 [1,] 212.7298 0.14 0.11
 [2,] 213.3778 0.14 0.11
 [3,] 214.0257 0.15 0.11
 [4,] 214.6737 0.15 0.12
 [5,] 215.3217 0.15 0.12
 [6,] 215.9696 0.15 0.12
 [7,] 216.6176 0.16 0.12
 [8,] 217.2656 0.16 0.13
 [9,] 217.9135 0.16 0.13
[10,] 218.5615 0.16 0.13
[11,] 219.2094 0.17 0.13

So, I need one vector getting the values from the third column given
specific values of the second:

s <- seq(0,1,0.05)
r <- NULL
for (w in 1:length(s)){
r[w] <- dados[,3][dados[,2]==s[w]][1]
}

This vector 'r' are working well to many values, but there are some
inconsistency (NA's) for others:

> r
 [1] 0.00 0.03 0.07   NA 0.16 0.21   NA   NA 0.36 0.41 0.46 0.52   NA 0.63  
NA
[16] 0.74 0.79   NA 0.90   NA 1.00

and dissecting this:

> s[4]
[1] 0.15
> dados[,3][dados[,2]==0.15][1]
[1] 0.11
> dados[,3][dados[,2]==s[4]][1]
[1] NA
> s[4]==0.15
[1] FALSE
> s[5]==0.20
[1] TRUE
> dados[,3][dados[,2]==s[5]][1]
[1] 0.16

Anyone could help me to discover why this is occorring?

dados[,2] and dados[,3] came from rounded values. Could this fact interfere
the results? 

Thanks in advance.

--
View this message in context: http://r.789695.n4.nabble.com/Getting-wrong-NA-values-using-for-cmd-tp3654335p3654335.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list