> foo <- as.character(c(0, 2))
> foo
[1] "0" "2"
> foo[1]
[1] "0"
> foo[2]
[1] "2"
> as.character("0-2")
[1] "0-2" #this is the output I want from the command below:
> as.character("foo[1]-foo[2]")
[1] "foo[1]-foo[2]" # ... was hoping to get "0-2"我尝试了一些eval(parse())的变体,但同样的问题。我也尝试了以下简单的例子:
> as.character("as.name(foo[1])")
[1] "as.name(foo[1])"
> as.character(as.name("foo[1]"))
[1] "foo[1]"是否有可能获得像as.character("foo[1]-foo[2]")这样简单的东西来显示"0-2"
更新
类似的示例(字符串要长得多):
> lol <- as.character(seq(0, 20, 2))
> lol
[1] "0" "2" "4" "6" "8" "10" "12" "14" "16" "18" "20"
> c(as.character("0-2"), as.character("2-4"), as.character("4-6"), as.character("6-8"), as.character("8-10"), as.character("10-12"), as.character("12-14"),as.character("14-16"),as.character("16-18"),as.character("18-20"))
[1] "0-2" "2-4" "4-6" "6-8" "8-10" "10-12" "12-14" "14-16" "16-18" "18-20"我希望能够从字符串中实际调用对象lol。
发布于 2016-02-17 06:20:01
我们可以将paste与collapse参数一起使用
paste(foo, collapse='-')
#[1] "0-2"如果我们需要将相邻的元素放在一起,删除'lol‘的第一个和最后一个元素,然后与paste参数一起paste它。
paste(lol[-length(lol)], lol[-1], sep='-')
#[1] "0-2" "2-4" "4-6" "6-8" "8-10" "10-12" "12-14" "14-16" "16-18"
#[10] "18-20"https://stackoverflow.com/questions/35449280
复制相似问题