R function that returns a string literal

浪子不回头ぞ 提交于 2020-01-03 10:48:23

问题


I have a vector: c(1,2,3)

Calling print() on this value gives [1] 1 2 3

Is there a function that takes a vector and gives the string c(1,2,3)?


回答1:


You can use deparse:

R> x <- c(1, 2, 3)
R> deparse(x)
[1] "c(1, 2, 3)"
R> class(deparse(x))
[1] "character"



回答2:


using dput:

a <- c(1, 2, 3);
dput(a)



回答3:


I've never heard of such a function. Perhaps you should quickly write one yourself?

toString <- function(a){
    output <- "c(";
    for(i in 1:(length(a)-1)){
        output <- paste(output, a[i], ",", sep="")
    }
    output <- paste(output, a[length(a)], ")\n", sep="")
    cat(output)
}


来源:https://stackoverflow.com/questions/1872982/r-function-that-returns-a-string-literal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!