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

Uwe Ligges ligges at statistik.tu-dortmund.de
Sat Jul 9 18:52:44 CEST 2011



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


> }
>
> Now, I get the right answer to r-vector
>
> Thanks for helping!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Getting-wrong-NA-values-using-for-cmd-tp3654335p3654548.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list