Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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
对对象使用expand.grid_R - Fatal编程技术网

对对象使用expand.grid

对对象使用expand.grid,r,R,我有以下向量和一个组合数据帧,它们是下面表达式的对象 x <- c(1,2,3,4) y <- c(5,6,7,8) z <- c(9,10,11,12) h <- data.frame(x,y,z) D <- print (( rep ( paste ( "h[,3]" ) , nrow(h) )) , quote=FALSE ) # [1] h[,3] h[,3] h[,3] h[,3] DD <- c ( print ( paste ( (D

我有以下向量和一个组合数据帧,它们是下面表达式的对象

x <- c(1,2,3,4)
y <- c(5,6,7,8)
z <- c(9,10,11,12)

h <- data.frame(x,y,z) 

D <- print (( rep ( paste ( "h[,3]" )  , nrow(h) )) , quote=FALSE )
# [1] h[,3] h[,3] h[,3] h[,3] 

DD <- c ( print ( paste ( (D) , collapse=","))) 
# "[1] h[,3],h[,3],h[,3],h[,3]"

DDD <- print ( DD, quote = FALSE ) 

# However when I place DDD in expand.grid it does not work

is(DDD)
[1] "character"  "vector"  "data.frameRowLabels"  "SuperClassMethod" 

x看起来您正在尝试生成一些R代码,然后执行它。对于您的情况,这将起作用:

# From your question
DDD
# [1] "h[,3],h[,3],h[,3],h[,3]"

# The code that you wish to execute, as a string
my_code <- paste("expand.grid(", DDD, ")")
# [1] "expand.grid( h[,3],h[,3],h[,3],h[,3] )"

# Execute the code
eval(parse(text = my_code))

顺便说一句,我建议查看
expand.grid
的帮助文件-在理解
expand.grid

的参数后,它们帮助我很快找到了问题的解决方案。看起来您正在尝试生成一些R代码,然后执行它。对于您的情况,这将起作用:

# From your question
DDD
# [1] "h[,3],h[,3],h[,3],h[,3]"

# The code that you wish to execute, as a string
my_code <- paste("expand.grid(", DDD, ")")
# [1] "expand.grid( h[,3],h[,3],h[,3],h[,3] )"

# Execute the code
eval(parse(text = my_code))

顺便说一句,我建议查看
expand.grid
的帮助文件-在理解了
expand.grid
的参数后,它们帮助我很快找到了问题的解决方案。grid

replicate
list
+
rep
更直接
expand.grid(replicate(3,h[,3],FALSE))
replicate
list
+
rep
更直接<代码>扩展.grid(复制(3,h[,3],FALSE))