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-tp18963310p18963310.html Sent from the R help mailing list archive at Nabble.com.
You can do this: ifelse(XY >= 0.8 | XY <= -0.8 | is.na(XY), XY, "low corr") On 8/13/08, Birgitle <birgit.lemcke at systbot.uzh.ch> 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-tp18963310p18963310.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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
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.