【问题标题】:Running the do.call() function without printing all of its argument运行 do.call() 函数而不打印其所有参数
【发布时间】:2021-05-08 02:04:46
【问题描述】:

当我运行 do.call() 时,它会打印很多我不想打印的东西。默认情况下,它会打印函数的源代码,以及我提供的完整参数。例如:

y = rnorm(100)
x = rnorm(100)
dat = data.frame(y,x)
do.call(lm, list(formula=as.formula('y~x'),data=dat))

最后一行按照我的意愿执行了 lm() 调用,但它也会打印出来:

Call:
(function (formula, data, subset, weights, na.action, method = "qr", 
    model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
    contrasts = NULL, offset, ...) 
{
    ret.x <- x
    ret.y <- y
    cl <- match.call()
    mf <- match.call(expand.dots = FALSE)
    m <- match(c("formula", "data", "subset", "weights", "na.action", 
        "offset"), names(mf), 0L)
    mf <- mf[c(1L, m)]
    mf$drop.unused.levels <- TRUE
    mf[[1L]] <- quote(stats::model.frame)
    mf <- eval(mf, parent.frame())
    if (method == "model.frame") 
        return(mf)
    else if (method != "qr") 
        warning(gettextf("method = '%s' is not supported. Using 'qr'", 
            method), domain = NA)
    mt <- attr(mf, "terms")
    y <- model.response(mf, "numeric")
    w <- as.vector(model.weights(mf))
    if (!is.null(w) && !is.numeric(w)) 
        stop("'weights' must be a numeric vector")
    offset <- model.offset(mf)
    mlm <- is.matrix(y)
    ny <- if (mlm) 
        nrow(y)
    else length(y)
    if (!is.null(offset)) {
        if (!mlm) 
            offset <- as.vector(offset)
        if (NROW(offset) != ny) 
            stop(gettextf("number of offsets is %d, should equal %d (number of observations)", 
                NROW(offset), ny), domain = NA)
    }
    if (is.empty.model(mt)) {
        x <- NULL
        z <- list(coefficients = if (mlm) matrix(NA_real_, 0, 
            ncol(y)) else numeric(), residuals = y, fitted.values = 0 * 
            y, weights = w, rank = 0L, df.residual = if (!is.null(w)) sum(w != 
            0) else ny)
        if (!is.null(offset)) {
            z$fitted.values <- offset
            z$residuals <- y - offset
        }
    }
    else {
        x <- model.matrix(mt, mf, contrasts)
        z <- if (is.null(w)) 
            lm.fit(x, y, offset = offset, singular.ok = singular.ok, 
                ...)
        else lm.wfit(x, y, w, offset = offset, singular.ok = singular.ok, 
            ...)
    }
    class(z) <- c(if (mlm) "mlm", "lm")
    z$na.action <- attr(mf, "na.action")
    z$offset <- offset
    z$contrasts <- attr(x, "contrasts")
    z$xlevels <- .getXlevels(mt, mf)
    z$call <- cl
    z$terms <- mt
    if (model) 
        z$model <- mf
    if (ret.x) 
        z$x <- x
    if (ret.y) 
        z$y <- y
    if (!qr) 
        z$qr <- NULL
    z
})(formula = y ~ x, data = structure(list(y = c(0.645552938260277, 
-1.03674741955656, -1.36678177623718, 0.518879099012354, -0.914092586089106, 
1.85633406839752, 2.33525903047248, 1.14722981159834, 0.275299886900546, 
-0.911857314399479, 0.906716193555437, -1.84262025693996, -0.721153566116692, 
2.48197346041889, 0.91637339376205, -0.0482959098521057, -0.412766187814658, 
0.565508110918711, 0.798593902512974, 1.02370248417386, 1.43489462095486, 
-0.00595661011583523, 0.750101041920158, 0.0298980087576892, 
0.578596357299012, 2.06230646195833, 0.837325836562161, -0.033216036814055, 
-0.584230631296736, -1.1266387975112, -0.689512161483645, 0.291619808691663, 
-1.69357030189337, 0.978250304761813, -0.755591443050875, 0.0581812013160168, 
0.160793605900764, 0.54544692679131, 2.67118590214976, 0.0423406641418217, 
0.441445795983119, -1.58961605562098, -0.22489452744228, -0.333387697959592, 
-0.606119646136784, -1.11971038685321, -0.515251196689386, 0.388206565971871, 
-1.19191379429206, -1.17626555070404, 0.549984711717793, 0.189356928489049, 
-0.732133641764621, 0.428942994232102, -1.10713109993258, -1.64799963728532, 
1.52406805730385, -0.817775798184812, -0.228579583992437, 0.222167620842928, 
-0.449100415184847, -1.37899113402036, -1.22746769483453, 1.69732446155693, 
0.910274215721166, -0.266266303066525, 0.000833984550746797, 
-0.693636029432195, 1.05678031077105, 0.765504099803622, -0.62851012512813, 
-1.02162531958929, -0.342983933183841, -0.614040542906127, -0.144903744412403, 
-1.86846800255061, -0.471608270196133, 0.132626367278681, 1.74544503775265, 
-0.471487649922146, 0.604438400086659, 0.825455235134079, 0.155961973523576, 
0.207420563250091, -0.130175758501042, 0.629838988210575, -0.787614987225754, 
-0.483446505017937, -0.248709520763105, 0.950993202262253, 0.825213623331804, 
-0.500480946731697, -1.37680920242343, 0.181153862811113, 0.408046651578768, 
0.341494326390166, 1.06940090717878, -1.22351447343963, -0.539163919598057, 
-0.766566843828881), x = c(0.59188040130602, 0.1051430615256, 
0.209172959698725, -0.191070094060143, -0.953114116953908, -1.33637617471067, 
-0.769931523816078, 1.19685219431872, 2.00578388289767, 0.0970818701412437, 
1.14507289348526, -0.667384225193717, -0.710354102271159, -0.326733273119207, 
1.50747585292862, 0.510175505755495, 0.432586204465598, 0.605913508749864, 
-0.0797253150799987, -0.289868926776903, 0.195719152585662, -1.08048882970833, 
-1.02806030503763, -0.955922907449149, -0.321714001459428, -0.0125625217985859, 
1.93972419212178, -1.2770087750596, -0.438044664481769, -0.16799844875219, 
0.737082811731615, -0.0217890205667605, -1.74348276484748, 0.924385228062877, 
0.41847902246872, -0.153295341243861, 1.50625650628283, -0.313786939328656, 
-0.72728516319703, 0.130203275918779, -0.48742154233068, -0.0373985569171018, 
0.571708971495553, -0.149970891031242, 0.0936771019912061, 0.678489222500144, 
1.43310528753968, 1.34167795827941, -9.67810025788311e-05, -2.81148656250494, 
-0.738982905660057, -2.01439275772803, 0.122587160911615, -0.68236104257436, 
0.814172850108428, -0.679019473616009, 0.9500158307568, -1.20692941419126, 
-0.245766342791828, -0.699289303631653, 0.840933442954149, -1.10606335516112, 
-0.807003021163596, -0.448282105485891, 0.514625936806471, -2.18234212156675, 
2.23461517185016, 0.614164437989616, -2.16424418381578, 2.10614632906954, 
-1.501290121483, 0.133628764048144, -0.371497644963806, -1.09326848929288, 
-0.876160219188844, 0.609731708125737, 1.71889085217346, 0.511989619195513, 
-1.69804077361953, -1.4931940305771, -1.41882351403557, 1.43631793534341, 
-0.329057398411564, 0.0188775594432232, 1.00838379625486, -0.0685665648915033, 
0.57463373913359, 0.153745024822862, 1.50108777993883, 1.35180164685218, 
1.30563013075212, -0.0934214107302569, 1.21967023498808, 2.71509985919089, 
-1.06079909507698, 0.104930565833438, -0.640079250392443, -0.896427447908767, 
-0.281678332849185, -1.31186538595782)), class = "data.frame", row.names = c(NA, 
-100L)))

Coefficients:
(Intercept)            x  
    0.01252      0.07049  

有没有办法让它不打印所有这些东西?

【问题讨论】:

    标签: r do.call


    【解决方案1】:

    使用 "lm" 而不是 lmquote(dat) 而不是 datas.formula('y~x') 也可以写成 y ~ x,尽管两者的输出相同。

    do.call("lm", list(y ~ x, data = quote(dat)))
    

    给予:

    Call:
    lm(formula = y ~ x, data = dat)
    
    Coefficients:
    (Intercept)            x  
        0.03577      0.01136  
    

    【讨论】:

      猜你喜欢
      • 2021-11-17
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      相关资源
      最近更新 更多