问题
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