Suppose these lines have already executed:
假设这些线已经执行:
allitems = c('B','N','D','C','E','M')
b = c(0,1)
Now, how can we cause expand.grid() to use those 2 variables to generate data.frame p, which is the equivalent grid of the following line of (working) code?
现在,我们如何使expand.grid()使用这两个变量来生成data.frame p,它相当于下面一行(工作)代码的网格?
p = expand.grid(B=b, N=b, D=b, C=b, E=b, M=b)
Thanks.
谢谢。
The functions expression() and eval() did not seem to help. Passing the string "B=b, N=b, D=b, C=b, E=b, M=b" to expand.grid() did not work either, although that solution would be perfectly acceptable too. I would prefer to use expand.grid() rather write something from scratch. But I would write something from scratch if I have to. It just seems like it would be a waste to completely discard the nice function expand.grid().
函数表达式()和eval()似乎没有帮助。将字符串“B= B, N= B, D= B, C= B, E= B, M= B”传递到expand.grid()也不起作用,尽管该解决方案也完全可以接受。我更喜欢使用expand.grid(),而不是从头开始编写。但如果有必要的话,我会从头开始写一些东西。完全丢弃这个漂亮的扩展.grid()函数似乎是一种浪费。
1 个解决方案
#1
4
Use do.call
:
使用do.call:
do.call(expand.grid,
setNames(rep(list(b), length(allitems)),
allitems))
#1
4
Use do.call
:
使用do.call:
do.call(expand.grid,
setNames(rep(list(b), length(allitems)),
allitems))