最近,我使用以下代码。
answer <- readline("please input the number.")
然后输入1:10,然后按Enter键。这里的答案= "1:10",如果有一种方法将字符"1:10“转换为数字1:10?
发布于 2014-04-18 16:59:44
你可以试试
x = '1:10'
inp = as.integer(strsplit(x, ':')[[1]])
inp[1]:inp[2]
[1] 1 2 3 4 5 6 7 8 9 10
它很丑,但没有附带的损害。
发布于 2014-04-18 17:04:38
我的回答是:看看eval(parse(text = ...))
。
基本想法是把它放在一个函数中,比如:
fun <- function() {
answer <- readline("Please input the number: ")
eval(parse(text = answer))
}
fun()
但是,一般情况下,在R中尽量避免使用eval(parse(text = ...))
类型的东西。
library(fortunes)
fortune(106)
https://stackoverflow.com/questions/23158473
复制相似问题