I have numeric objects a=1,b=2,c=3,d=4. Now when I use :
toString(c(a,b,c,d))
I get:
"1, 2, 3, 4"
as the output. How do I get rid of the comma? I want "1234" as the output. Or is there another way to do this?
For user-defined Function objects, the toString method returns a string containing the source text segment which was used to define the function. JavaScript calls the toString method automatically when a Function is to be represented as a text value, e.g. when a function is concatenated with a string.
toString() function in R Language is used to convert an object into a single character string.
To convert elements of a Vector to Strings in R, use the toString() function. The toString() is an inbuilt R function used to produce a single character string describing an R object.
Just use paste
or paste0
:
a <- 1; b <- 2; c <- 3; d <- 4
paste0(a, b, c, d)
# [1] "1234"
paste(a, b, c, d, sep="")
# [1] "1234"
You cannot get the result directly from toString
even though toString
uses paste
under the hood:
toString.default
# function (x, width = NULL, ...)
# {
# string <- paste(x, collapse = ", ")
# --- function continues ---
Compare that behavior with:
paste(c(a, b, c, d), collapse = ", ")
# [1] "1, 2, 3, 4"
Since it is hard-coded, if you really wanted to use toString
, you would have to then use sub
/gsub
to remove the ",
" after you used toString
, but that seems inefficient to me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With