【发布时间】:2016-07-09 03:49:00
【问题描述】:
我想读取R 到Rscript 中的命令行参数,并将其中存储的值用于一些整数运算。默认情况下,命令行参数作为字符导入:
#!/usr/bin/env Rscript
arg <- commandArgs(trailingOnly = TRUE)
x = as.vector(arg[1])
class(x)
x
y = as.vector(arg[2])
class(y)
y
cor.test(x,y)
这是此脚本的输出:
$ Rscript Correlation.R 3,3,2 4,8,6
[1] "character"
[1] "3,3,2"
[1] "character"
[1] "4,8,6"
Error in cor.test.default(x, y) : 'x' must be a numeric vector
Calls: cor.test -> cor.test.default
Execution halted
如何将 x 和 y 转换为数值向量?
【问题讨论】: