hi, Just a very simple question: is there an R equivalent to the matlab command find(X) which returns the indices of vector X that store non-zero elements? e.g.> find( [1 0 0 1 0])ans 1 4 so, in R, how do I do: ans <- rfind( c(1,0,0,1,0)) so that ans is the vector c(1,4) thanks, stephen -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Stephen Eglen <stephen at cogsci.ed.ac.uk> writes:> so, in R, how do I do: > > ans <- rfind( c(1,0,0,1,0)) > > so that ans is the vector c(1,4)which( c(1,0,0,1,0) != 0 ) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, 8 Jun 2000, Stephen Eglen wrote:> hi, > > Just a very simple question: is there an R equivalent to the matlab > command find(X) which returns the indices of vector X that store > non-zero elements? > > e.g. > > find( [1 0 0 1 0]) > ans > > 1 4 > > so, in R, how do I do: > > ans <- rfind( c(1,0,0,1,0)) > > so that ans is the vector c(1,4)rfind <- function(x) seq(along=x)[x != 0] It's a peculiar name, though .... -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
x<-c(1, 0, 0, 1, 0) which(x!=0) Bill Simpson -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Stephen Eglen wrote:> > hi, > > Just a very simple question: is there an R equivalent to the matlab > command find(X) which returns the indices of vector X that store > non-zero elements? > > e.g. > > find( [1 0 0 1 0]) > ans > > 1 4 > > so, in R, how do I do: > > ans <- rfind( c(1,0,0,1,0)) > > so that ans is the vector c(1,4)Sorry, first answer was wrong! Try: which(x!=0) Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, 8 Jun 2000, Stephen Eglen wrote:>so, in R, how do I do: > >ans <- rfind( c(1,0,0,1,0)) > >so that ans is the vector c(1,4)You can do:> which(c(1,0,0,1,0) != 0)[1] 1 4 Best, Kjetil -- Kjetil Kjernsmo Graduate astronomy-student Problems worthy of attack University of Oslo, Norway Prove their worth by hitting back E-mail: kjetikj at astro.uio.no - Piet Hein Homepage <URL:http://www.astro.uio.no/~kjetikj/> Webmaster at skepsis.no -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Do you mean this:> x <- c(1,0,0,1) > which(x!=1)[1] 1 4 ? Peter.> command find(X) which returns the indices of vector X that store > non-zero elements? > > e.g. > > find( [1 0 0 1 0]) > ans** To YOU I'm an atheist; to God, I'm the Loyal Opposition. Woody Allen ** P.Malewski Tel.: 0531 500965 Maschplatz 8 Email: P.Malewski at tu-bs.de ************************38114 Braunschweig******************************** -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
With R-1.0.1, Linux RH6.2 and survival5, I try> x2.fit <- survfit(Surv(stopp, event) ~ x2) > plot(x2.fit, fun = "cloglog")Error in rep(2, n2 - 1) : invalid number of copies in "rep" In addition: Warning message: 1 x value <= 0 omitted from logarithmic plot in: xy.coords(x, y, xlabel, ylabel, log) (an empty plot area pops up), also> print(x2.fit)Call: survfit(formula = Surv(stopp, event) ~ x2) n events mean se(mean) median 0.95LCL 0.95UCL x2=0 52 23 3.19 0.514 2.277 0.839 Inf x2=1 48 40 0.69 0.173 0.282 0.169 0.66 Warning message: no finite arguments to min/max; returning extreme. in: min(..., na.rm na.rm) seem to indicate something strange. However, 'fun="cumhaz"' works as expected. Is this a bug, or am I doing something stupid (again)? I tried to replicate this with a small data set:> t1[1] 1.233 0.391 0.219 0.420 0.710 1.658 1.487 2.888 0.175 0.045> ev[1] 1 1 1 1 1 1 1 1 1 1> fit <- survfit(Surv(t1, ev) ~ 1) > plot(fit, fun = "cloglog")Error in rep(2, n2 - 1) : invalid number of copies in "rep" In addition: Warning message: 1 x value <= 0 omitted from logarithmic plot in: xy.coords(x, y, xlabel, ylabel, log) However, this time a plot (apparently correct) shows up. G?ran -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._