[R] Help with for
Thomas Lumley
thomas at biostat.washington.edu
Tue Feb 15 01:25:49 CET 2000
On Mon, 14 Feb 2000, a s wrote:
> I haven't been able to solve two issues, really appreciate any help.
>
> Q1. Why this one goes like that? (could it be a bug?):
>
> What I want:
> > J_c(1,2,3)
> > MA_NA
> > MA[1]_min(J[1:2])
> > MA[2]_min(J[2:3])
> > MA
> [1] 1 2
>
> Good, so now lets do it a little more faster (since the real vectors have
> over 15000 obs):
>
> > for (i in 1:2) MA[i]_min(J[i:i+1])
> > MA
> [1] 2 3
>
> It does the same with any function instead of min.
The problem is that i:i+1 should be i:(i+1)
eg
R> i<-10
R> i:i+1
[1] 11
R> i:(i+1)
[1] 10 11
i:i+1 means (i:i)+1, which reduces to i+1
>
> Q2. The former type of job works better with sapply (or the like), but when
> FUN returns a vector, I haven't been able to get a matrix instead of a list
> (simplify=TRUE, does not make any difference). Any way to avoid converting
> list into matrix after sapply??
I don't know what you mean. The examples for sapply() include such an
example, and it works.
R> x <- list(a = 1:10, beta = exp(-3:3), logic = c(T,F,F,T))
R> sapply(x, quantile)
a beta logic
0% 1.00 0.04978707 0.0
25% 3.25 0.25160736 0.0
50% 5.50 1.00000000 0.5
75% 7.75 5.05366896 1.0
100% 10.00 20.08553692 1.0
-thomas
Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list