对对象使用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
的参数后,它们帮助我很快找到了问题的解决方案。gridreplicate
比list
+rep
更直接expand.grid(replicate(3,h[,3],FALSE))
replicate
比list
+rep
更直接<代码>扩展.grid(复制(3,h[,3],FALSE))
。