tags:

views:

96

answers:

3

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)"?

A: 

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)
}
noname
+7  A: 

You can use deparse:

R> x <- c(1, 2, 3)
R> deparse(x)
[1] "c(1, 2, 3)"
R> class(deparse(x))
[1] "character"
rcs
Thanks for the help! I just started using R and it's pretty frustrating knowing exactly what I want to do, but not knowing what R calls it.
hekevintran
+3  A: 

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

mdsumner