[英]passing command line arguments to anova
Simple problem, but only examples I can find of passing command line arguments use 'file=' and not any formulas. 这个问题很简单,但只有通过命令行参数传递的示例可以使用'file ='而不是任何公式。
eg data.txt 例如data.txt
id var1 group
1 5 1
2 6 1
3 4 1
4 12 2
5 14 2
6 20 2
Why doesn't this work to specify the group variable to anova in the command line: ./anova.R data.txt group 为什么在命令行中将组变量指定为anova无效:./anova.R data.txt group
#!/usr/bin/env Rscript
args <- commandArgs(trailingOnly=TRUE)
data1 <- read.table(args[1],sep="\t", header =TRUE)
result <- summary(aov(richness ~ args[2], data=data1))
write("richness",file="alphatests.txt",append=TRUE)
capture.output(result, file="alphatests.txt",append=TRUE)
variable lengths differ (found for 'args[2]') Calls: summary ... -> eval -> eval -> -> model.frame.default Execution halted
可变长度不同(为'args [2]'找到)调用:summary ...-> eval-> eval->-> model.frame.default暂停执行
But this does work (when there is a column name 'group' in both examples): 但这确实有效(当两个示例中都有一个列名“ group”时):
#!/usr/bin/env Rscript
args <- commandArgs(trailingOnly=TRUE)
data1 <- read.table(args[1],sep="\t", header =TRUE)
result <- summary(aov(richness ~ group, data=data1))
write("richness",file="alphatests.txt",append=TRUE)
capture.output(result, file="alphatests.txt",append=TRUE)
Why can't I pass a command line argument to a formula? 为什么不能将命令行参数传递给公式?
We can use standard methods for changing the formula 我们可以使用标准方法来更改公式
result <- summary(aov(reformulate(args[2], 'richness'), data = data1))
-fullcode -fullcode
#!/usr/bin/env Rscript
args <- commandArgs(trailingOnly=TRUE)
data1 <- read.table(args[1], header =TRUE)
result <- summary(aov(reformulate(args[2], 'richness'), data = data1))
print(result)
-run the script in terminal -在终端中运行脚本
$ Rscript anova.R data.txt group
# Df Sum Sq Mean Sq F value Pr(>F)
#group 1 160.17 160.17 17.47 0.0139 *
#Residuals 4 36.67 9.17
#---
#Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
NOTE: Assuming the second column name to be 'richness' in data1.txt 注意:假设第二列名称为data1.txt中的“丰富性”
Command-line arguments are returned as strings so it does not work when you pass it to aov
function. 命令行参数以字符串形式返回,因此当您将其传递给
aov
函数时, aov
不起作用。 One workaround is to use get
一种解决方法是使用
get
result <- summary(aov(richness ~ get(args[2]), data=data1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.