dear lists, My question is quite simple but i'm a new user in R and it's seem tough to me. How am i going to recall back my values??ok..it could be easy if i ilustrate my problem with the simple example. say that, x [1] 1 2 y [1] 2 3 and my logical output from my function are (Below is my function): overlapped(x,y) [1] TRUE [1] TRUE suppose i want my output to be: [1] 1 2 [1] 2 3 overlapped<-function(x,y){ z<-rbind(x,y) for(i in 1:nrow(z)){ overlap<-(z[i,1]<=z[,2]&z[i,2]>=z[,1])[-i] if(any(overlap)) print(TRUE) else print(FALSE) } } Thanks in advance!! cheers, Anisah --------------------------------- [[alternative HTML version deleted]]
Henrique Dallazuanna
2008-Feb-17 22:14 UTC
[R] extracting elements by using logical values
Try this: overlapped<-function(x,y) { z<-rbind(x,y) overlap <- vector() for(i in 1:nrow(z)){ overlap<-c(overlap, (z[i,1]<=z[,2]&z[i,2]>=z[,1])[-i]) } return(z[overlap,]) } On 17/02/2008, mohamed nur anisah <nuranisah_mohamed at yahoo.com> wrote:> dear lists, > > My question is quite simple but i'm a new user in R and it's seem tough to > me. How am i going to recall back my values??ok..it could be easy if i > ilustrate my problem with the simple example. say that, > x > [1] 1 2 > y > [1] 2 3 > > and my logical output from my function are (Below is my function): > > overlapped(x,y) > [1] TRUE > [1] TRUE > > suppose i want my output to be: > [1] 1 2 > [1] 2 3 > > > overlapped<-function(x,y){ > z<-rbind(x,y) > for(i in 1:nrow(z)){ > overlap<-(z[i,1]<=z[,2]&z[i,2]>=z[,1])[-i] > if(any(overlap)) print(TRUE) > else print(FALSE) > } > } > > Thanks in advance!! > > cheers, > Anisah > > > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > 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