

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.
Victor Delgado
Professor in department of Economics,
UFOP  Univ. Federal de Ouro Preto, Brazil


Looks like FAQ 7.31 to me.
Try all.equal() instead of ==.
Sarah
On Fri, Jul 8, 2011 at 11:06 AM, VictorDelgado
< [hidden email]> wrote:
> 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.
>
> 

Sarah Goslee
http://www.functionaldiversity.org______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


ty S. Goslee,
It's helpfull to test the condition:
> all.equal(s[4],0.15)
[1] TRUE
instead the previous "FALSE" answer obtained with
>s[4]==0.15
[1] FALSE
but I still need get it to vector r:
Victor Delgado wrote
> for (w in 1:length(s)){
> r[w] < dados[,3][dados[,2]==s[w]][1]
> }
I just managed this, writing:
s < seq(0,1,0.05)
r < NULL
for (w in 1:length(s)){
r[w] < dados[,3][dados[,2]== round(s[w],2)][1]
}
Now, I get the right answer to rvector
Thanks for helping!
Victor Delgado
Professor in department of Economics,
UFOP  Univ. Federal de Ouro Preto, Brazil


On 08.07.2011 18:20, VictorDelgado wrote:
> ty S. Goslee,
>
> It's helpfull to test the condition:
>
>> all.equal(s[4],0.15)
> [1] TRUE
>
> instead the previous "FALSE" answer obtained with
>
>> s[4]==0.15
> [1] FALSE
>
> but I still need get it to vector r:
>
> Victor Delgado wrote:
>>
>>
>>> for (w in 1:length(s)){
>>> r[w]< dados[,3][dados[,2]==s[w]][1]
>>> }
>>
>
> I just managed this, writing:
>
> s< seq(0,1,0.05)
> r< NULL
> for (w in 1:length(s)){
Generally use seq_along(s) rather than 1:length(s) since the former is a
lot safer (not here, but in general).
> r[w]< dados[,3][dados[,2]==*round(s[w],2)*][1]
r[w] < dados[mapply(function(x,y) isTRUE(all.equal(x,y)), dados[,2],
s[w]),3][1]
seems to be cleaner.
Uwe Ligges
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

