[R] using list to pass argument to function
Henrik Bengtsson
hb at maths.lth.se
Wed Nov 13 09:58:41 CET 2002
The problem is that c() is of length zero and repeating such a vector R
times will only give you a vector of length zero, e.g. length(rep(c(),
R)) == 0. I think you have observed this already. The only way to store
a repetition of empty vectors or NULL values is by using the data type
list, e.g.
args <- rep(list(c()), R)
Then sapply(args, FUN=anotherfunction) will work, but I think you are
better of with
lapply(args, FUN=anotherfunction)
I am not sure if you wrote the function that returns c() or not. If not,
you can always convert a vector to a list and then make sure it has the
right length, i.e. R, by doing:
if (length(vec) == 0)
args <- rep(list(vec), length.out=R)
else
args <- rep(as.list(vec), length.out=R)
Warning, only using as.list() won't work if length(vec) == 0, but if you
know that it is not a list that is returned, which I guess you know, all
you have to do is:
args <- rep(list(vec), length.out=R)
Finally, if R is not way too big I actually think a for loop would be
equally fast if that is your concern. Someone correct me if I am wrong,
but for loops are not penalized that much in R as for instance in
Matlab.
Cheers
Henrik Bengtsson
> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch
> [mailto:owner-r-help at stat.math.ethz.ch] On Behalf Of
> TyagiAnupam at aol.com
> Sent: den 13 november 2002 13:02
> To: r-help at stat.math.ethz.ch
> Subject: [R] using list to pass argument to function
>
>
> Hi,
>
> I am trying to apply *sapply* to functions within functions
> within functions
> to do a Monte Carlo experiment: take Y sample of size X based on cell
> probabilities (using Brian Ripley's suggestion for drawing
> from contingency
> tables), for each calculate a vector of statistics, calculate
> bootstrap std
> errs with R replications and their means and SD. Store
> results in a matrix
> for further use. The problem is how to pass 3 arguments
> returned as c() by a
> fucntion to another function that uses them using:
> sapply(rep(c(), R),
> anotherfucntion). This does not seem to work, possibly
> because I am passing a
> c() argument where 3 separate arguemnts are needed. What is a
> good way to do
> this? I am trying to avoid *for* loops.
>
> Anupam.
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
>
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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