[R] How to pass value to an argument in a function which is an argument to the main function
David Winsemius
dwinsemius at comcast.net
Fri May 7 23:05:19 CEST 2010
On May 7, 2010, at 4:18 PM, Jun Shen wrote:
> David,
>
> my.boxplot.stats is modified from boxplot.stats (package grDevices)
> as follows. x is the original argument, I guess it's the data object
> for processing. I only added "type". Thanks.
Right, but you should be assigning stats=my.boxplot.stats, not
stats=myboxplot.stats(type=6). The functions then pick up their
arguments from their environment.
(And learn to send plain text. It is very easy to do so in gmail.)
--
David.
>
> Jun
>
> =============================================================
> > boxplot.stats
>
> function (x, coef = 1.5, do.conf = TRUE, do.out = TRUE)
> {
> if (coef < 0)
> stop("'coef' must not be negative")
> nna <- !is.na(x)
> n <- sum(nna)
> stats <- stats::fivenum(x, na.rm = TRUE)
> iqr <- diff(stats[c(2, 4)])
> if (coef == 0)
> do.out <- FALSE
> else {
> out <- if (!is.na(iqr)) {
> x < (stats[2L] - coef * iqr) | x > (stats[4L] + coef *
> iqr)
> }
> else !is.finite(x)
> if (any(out[nna], na.rm = TRUE))
> stats[c(1, 5)] <- range(x[!out], na.rm = TRUE)
> }
> conf <- if (do.conf)
> stats[3L] + c(-1.58, 1.58) * iqr/sqrt(n)
> list(stats = stats, n = n, conf = conf, out = if (do.out) x[out &
> nna] else numeric(0L))
> }
> <environment: namespace:grDevices>
>
>
> On Fri, May 7, 2010 at 3:09 PM, David Winsemius <dwinsemius at comcast.net
> > wrote:
>
> On May 7, 2010, at 3:30 PM, Jun Shen wrote:
>
> Dear all,
>
> I constructed this function called my.boxplot.stats by replacing
> fivnum()
> with quantile() in function boxplot.stats(). So I can try different
> quantile
> methods in bwplot(). The problem is I couldn't pass different values
> to the
> "type" argument to my.boxplot.stats, which in turn is an argument in
> bwplot(). Now I just have to manually change the "type" value in
> my.boxplot.stats. What I would like to do is call bwplot() like
>
> bwplot(CL~DOSE,data=patab,stats=my.boxplot.stats(type=7)),
>
>
> You have defined a function with an argument of x that does not have
> a default value, so it throws an error when you call it with no
> value for x. (It's not complaining about the type argument.)
>
> --
> David.
>
>
> But I got an error:
> Error in my.boxplot.stats(type = 7) : element 1 is empty;
> the part of the args list of 'is.na' being evaluated was:
> (x)
>
> Thanks a lot.
>
> Jun Shen from Millipore Corporation
>
> ========================================================
> my.boxplot.stats<-function (x, coef = 1.5, *type=6*, do.conf = TRUE,
> do.out
> = TRUE)
> {
> if (coef < 0)
> stop("'coef' must not be negative")
> nna <- !is.na(x)
> n <- sum(nna)
> stats <- stats::quantile(x, type=type,na.rm = TRUE)
> iqr <- diff(stats[c(2, 4)])
> if (coef == 0)
> do.out <- FALSE
> else {
> out <- if (!is.na(iqr)) {
> x < (stats[2L] - coef * iqr) | x > (stats[4L] + coef *
> iqr)
> }
> else !is.finite(x)
> if (any(out[nna], na.rm = TRUE))
> stats[c(1, 5)] <- range(x[!out], na.rm = TRUE)
> }
> conf <- if (do.conf)
> stats[3L] + c(-1.58, 1.58) * iqr/sqrt(n)
> list(stats = stats, n = n, conf = conf, out = if (do.out) x[out &
> nna] else numeric(0L))
> }
>
> [[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.
>
> David Winsemius, MD
> West Hartford, CT
>
>
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list