R “发送”;“未分类”;变量作为函数参数?

R “发送”;“未分类”;变量作为函数参数?,r,ellipsis,function-call,function-parameter,R,Ellipsis,Function Call,Function Parameter,或 如何创建我自己的“…”变量? 函数通常返回特定的结构(或类)。有时函数的参数也是返回结构的一部分。操纵结果后,如何将其作为参数发送给函数(或再次发送给同一函数) 非常简短的伪示例: res1 <- power.t.test(alot of parameters) res1b <- res1 res1b$some.parameter <- new-value res2 <- power.t.test(parameters = unclass(res1b)) res1这

如何创建我自己的“…”变量?

函数通常返回特定的结构(或类)。有时函数的参数也是返回结构的一部分。操纵结果后,如何将其作为参数发送给函数(或再次发送给同一函数)

非常简短的伪示例:

res1 <- power.t.test(alot of parameters)
res1b <- res1
res1b$some.parameter <- new-value
res2 <- power.t.test(parameters = unclass(res1b))

res1这里有一个可能不是最优的解决方案,将结果指定为所有参数和结果的列表,并使用第一个参数的
unlist
处理缺少的参数:


af这里有一个可能不是最优的解决方案,将结果指定为所有参数和结果的列表,并使用第一个参数的
unlist
处理缺少的参数:


af您可以使用
purrr::lift
将函数从接受参数作为
更改为接受列表:

## power.t.test2 accepts arguments as a single list
power.t.test2 <- purrr::lift( power.t.test )

## Modify outputs as needed
ptt1$n <- ceiling(ptt1$n)
ptt1$power <- NULL

## Reduce to a set of arguments that are accepted by the function
a <- intersect(names(ptt1), formalArgs(power.t.test))

## Use the version that accepts the list
power.t.test2( ptt1[a] )
##power.t.test2将参数作为单个列表接受

power.t.test2您可以使用
purrr::lift
将函数从接受参数作为
更改为接受列表:

## power.t.test2 accepts arguments as a single list
power.t.test2 <- purrr::lift( power.t.test )

## Modify outputs as needed
ptt1$n <- ceiling(ptt1$n)
ptt1$power <- NULL

## Reduce to a set of arguments that are accepted by the function
a <- intersect(names(ptt1), formalArgs(power.t.test))

## Use the version that accepts the list
power.t.test2( ptt1[a] )
##power.t.test2将参数作为单个列表接受

power.t.test2我知道你已经接受了另一个答案,但只是为了记录在案:

do.call
是调用函数并传递列表而不是单个参数的方法。在这种情况下,它将如下所示:

do.call(power.t.test,as.list(ptt1))
如果只想传递某些参数,可以使用函数参数列表中显示的名称对向量进行子集:

do.call(power.t.test,as.list(ptt1[names(ptt1) %in% formalArgs(power.t.test)]))

我知道你已经接受了另一个答案,但只是记录在案:

do.call
是调用函数并传递列表而不是单个参数的方法。在这种情况下,它将如下所示:

do.call(power.t.test,as.list(ptt1))
如果只想传递某些参数,可以使用函数参数列表中显示的名称对向量进行子集:

do.call(power.t.test,as.list(ptt1[names(ptt1) %in% formalArgs(power.t.test)]))

我不确定,但我想你要找的是
do.call(power.t.test,as.list(ptt1))
@iod-这很有效!但是我必须调整一些:ptt1$方法有没有办法让函数调用忽略未使用的参数?因此,我以前不需要清理变量。@iod-你能写下你的评论作为答案吗(因为它主要回答了我的问题):我不确定,但我想你要找的是
do.call(power.t.test,as.list(ptt1))
@iod-这很有效!但是我必须调整一些:ptt1$方法有没有办法让函数调用忽略未使用的参数?因此,我不需要在之前清理变量。@iod-你能写下你的评论作为回答吗(因为它主要回答了我的问题):d谢谢你的回答!不过我在理解上有点困难。我认为@iod的答案更接近我所寻找的。谢谢你的回答!不过我在理解上有点困难。我认为@iod的答案更接近我所寻找的。