将命令行参数传递给方差分析
passing command line arguments to anova
简单的问题,但我只能找到使用 'file=' 而非任何公式传递命令行参数的示例。
例如data.txt
id var1 group
1 5 1
2 6 1
3 4 1
4 12 2
5 14 2
6 20 2
为什么在命令行中将组变量指定为方差分析不起作用:./anova.R data.txt 组
#!/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
但这确实有效(当两个示例中都有列名 '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)
为什么我不能将命令行参数传递给公式?
Command-line 参数作为字符串返回,因此当您将它传递给 aov
函数时它不起作用。一种解决方法是使用 get
result <- summary(aov(richness ~ get(args[2]), data=data1))
我们可以使用标准方法来更改公式
result <- summary(aov(reformulate(args[2], 'richness'), data = data1))
-完整代码
#!/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)
-运行终端中的脚本
$ 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
注意:假设第二列名称为 'richness' in data1.txt
简单的问题,但我只能找到使用 'file=' 而非任何公式传递命令行参数的示例。
例如data.txt
id var1 group
1 5 1
2 6 1
3 4 1
4 12 2
5 14 2
6 20 2
为什么在命令行中将组变量指定为方差分析不起作用:./anova.R data.txt 组
#!/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
但这确实有效(当两个示例中都有列名 '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)
为什么我不能将命令行参数传递给公式?
Command-line 参数作为字符串返回,因此当您将它传递给 aov
函数时它不起作用。一种解决方法是使用 get
result <- summary(aov(richness ~ get(args[2]), data=data1))
我们可以使用标准方法来更改公式
result <- summary(aov(reformulate(args[2], 'richness'), data = data1))
-完整代码
#!/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)
-运行终端中的脚本
$ 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
注意:假设第二列名称为 'richness' in data1.txt