我有一个函数,它基本上从参数中输出一个布尔条件作为字符串(函数的细节在这里无关紧要)
makeClause <-function(Sex=c("NA", "male", "female"),
SmokingHx=c("NA", "current", "former", "never"),
conjunction=c("&", "|")) {
arglist = as.list(match.call())
return(arglist)
}
我有一个包含所有输入参数组合的数据框,如:
Sex SmokingHx conjunction
1 NA NA &
2 Male NA &
...
我以这种方式获得:
combinations = expand.grid(Sex=c("NA", "male", "female"),
SmokingHx=c("NA", "current", "former", "never"),
conjunction=c("&", "|"),
stringsAsFactors=FALSE)
我打电话makeClause
给mapply
:
mapply(makeClause, Sex=combinations$Sex,SmokingHx=combinations$SmokingHx, conjunction=combinations$conjunction)
查看arglist
我得到的变量:
$Sex
dots[[1L]][[1L]]
$SmokingHx
dots[[2L]][[1L]]
$conjunction
dots[[4L]][[1L]]
如果不是as.list(match.call())
我打电话as.list(environment())
,而是我得到:
$Sex
[1] "male"
$SmokingHx
[1] "NA"
$conjunction
dots[[4L]][[1L]] # notice this is the only one for which I don't get the actual string
所以我有两个问题:
- 您能否解释导致将其作为参数值而不是实际字符串值的 R 内部结构?
- 我该如何解决这个问题,即获取参数列表中的字符串值?
谢谢