Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 更改a";调用;对象_R - Fatal编程技术网

R 更改a";调用;对象

R 更改a";调用;对象,r,R,我有一个call对象,我想添加一个参数,但我不想以这种方式使用parse 假设我有一个lm对象,那么来自lm lma <- lm(mpg ~ cyl, data=mtcars) lma$call # lm(formula = mpg ~ cyl, data = mtcars) 但是这对lma$call不起作用,因为lma$call中没有权重参数 因此,我觉得我应该能够通过添加另一个元素来简单地“增长”调用,但我不知道如何做到这一点。例如,以下操作失败: cl <- lma$call

我有一个call对象,我想添加一个参数,但我不想以这种方式使用parse

假设我有一个
lm
对象,那么来自
lm

lma <- lm(mpg ~ cyl, data=mtcars)
lma$call
# lm(formula = mpg ~ cyl, data = mtcars)
但是这对
lma$call
不起作用,因为
lma$call
中没有权重参数

因此,我觉得我应该能够通过添加另一个元素来简单地“增长”调用,但我不知道如何做到这一点。例如,以下操作失败:

cl <- lma$call
cl <- c(cl, weights=quote(wt))
eval(cl)
# [[1]]
# lm(formula = mpg ~ cyl, data = mtcars)
#
# $weights
# wt

cl我相信pryr包提供了一些有用的函数来处理调用:

lma <- lm(mpg ~ cyl, data=mtcars)
lm_call <- lma$call

library(pryr)
modify_call(lm_call,list(weights = runif(32)))

> lm_call2 <- modify_call(lm_call,list(weights = runif(32)))
> eval(lm_call2)

Call:
lm(formula = mpg ~ cyl, data = mtcars, weights = c(0.934802365722135, 
0.983909613220021, 0.762353664264083, 0.23217184189707, 0.850970500381663, 
0.430563687346876, 0.962665138067678, 0.318865151610225, 0.697970792884007, 
0.389103061752394, 0.824285467388108, 0.676439745584503, 0.344414771301672, 
0.292265978176147, 0.925716639030725, 0.517001488478854, 0.726312294835225, 
0.842773627489805, 0.669753148220479, 0.618112818570808, 0.139365098671988, 
0.843711007386446, 0.851153723662719, 0.134744396666065, 0.92681276681833, 
0.00274682720191777, 0.732672147220001, 0.4184603120666, 0.0912447033915669, 
0.427389309043065, 0.721000595251098, 0.614837386412546))

Coefficients:
(Intercept)          cyl  
     38.508       -2.945  

lma我相信pryr包提供了一些有用的函数来处理调用:

lma <- lm(mpg ~ cyl, data=mtcars)
lm_call <- lma$call

library(pryr)
modify_call(lm_call,list(weights = runif(32)))

> lm_call2 <- modify_call(lm_call,list(weights = runif(32)))
> eval(lm_call2)

Call:
lm(formula = mpg ~ cyl, data = mtcars, weights = c(0.934802365722135, 
0.983909613220021, 0.762353664264083, 0.23217184189707, 0.850970500381663, 
0.430563687346876, 0.962665138067678, 0.318865151610225, 0.697970792884007, 
0.389103061752394, 0.824285467388108, 0.676439745584503, 0.344414771301672, 
0.292265978176147, 0.925716639030725, 0.517001488478854, 0.726312294835225, 
0.842773627489805, 0.669753148220479, 0.618112818570808, 0.139365098671988, 
0.843711007386446, 0.851153723662719, 0.134744396666065, 0.92681276681833, 
0.00274682720191777, 0.732672147220001, 0.4184603120666, 0.0912447033915669, 
0.427389309043065, 0.721000595251098, 0.614837386412546))

Coefficients:
(Intercept)          cyl  
     38.508       -2.945  

lma谢谢你的提示。在Wickham的AdvancedR中,在表达式下搜索这一点可以找到一个更简单的解决方案:调用:修改调用(在线)<代码>cl谢谢你的提示。在Wickham的AdvancedR中,在表达式下搜索这一点可以找到一个更简单的解决方案:调用:修改调用(在线)<代码>cl