[R] Tiny help for tiny function

Birgitle birgit.lemcke at systbot.uzh.ch
Wed Aug 13 16:13:46 CEST 2008


Many thanks.

Much easier than my solution

B.


Birgitle wrote:
> 
> I just started to write tiny functions and therefore I appologise in
> advance if I am asking stupid question.
> 
> I wrote a tiny function to give me back from the original matrix, a matrix
> showing only the values smaller -0.8 and bigger 0.8.
> 
> y<-c(0.1,0.2,0.3,-0.8,-0.4,0.9)	
> x<-c(0.5,0.3,0.9,-0.9,-0.7,0.3)
> 
>  XY<-rbind(x,y)
> 
> extract.values<-function (x)
> {
> if(x>=0.8|x<=-0.8)x
> else("low corr.")
> 	
> 	}
> 
> works:
> 
> Test<-sapply(XY,extract.values,simplify=FALSE)
> 
> but now I try to solve the problem of having NA in the matrix.
> I tried like that:
> 
> extract.values<-function (x)
> {
> if(x>=0.8|x<=-0.8|x==NA)x
> else("low corr.")
> 	
> 	}
> 
> woks not:
> 
> x<-c(0.5,0.3,0.9,-0.9,-0.7,0.3)
> y<-c(0.1,0.2,NA,-0.8,-0.4,0.9)
> 
> XY<-rbind(x,y)
> 
> Testi<-sapply(XY,extract.values,simplify=FALSE)
> 
> Fehler in if (x >= 0.8 | x <= -0.8 | x == NA) x else ("low corr.") : 
>   Fehlender Wert, wo TRUE/FALSE nötig ist
> 
> Error in if (x >= 0.8 | x <= -0.8 | x == NA) x else ("low corr.") : 
>   Missing value, where TRUE/FALSE is needed
> 
> How can I do this right.
> 
> Thanks for help
> 
> B.
> 


-----
The art of living is more like wrestling than dancing.
(Marcus Aurelius)
-- 
View this message in context: http://www.nabble.com/Tiny-help-for-tiny-function-tp18963310p18963906.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list