在 R 中使用 toString 函数 [英] Using toString function in R
问题描述
我有数字对象 a=1,b=2,c=3,d=4.现在当我使用:
I have numeric objects a=1,b=2,c=3,d=4. Now when I use :
toString(c(a,b,c,d))
我明白了:
"1, 2, 3, 4"
作为输出.如何去掉逗号?我想要1234"作为输出.或者有其他方法可以做到这一点?
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?
推荐答案
只需使用 paste
或 paste0
:
a <- 1; b <- 2; c <- 3; d <- 4
paste0(a, b, c, d)
# [1] "1234"
paste(a, b, c, d, sep="")
# [1] "1234"
<小时>
你不能直接从 toString
得到结果,即使 toString
在幕后使用了 paste
:
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"
由于它是硬编码的,如果你真的想使用toString
,你就必须使用sub
/gsub
来删除使用 toString
后的,
",但这对我来说似乎效率低下.
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.
这篇关于在 R 中使用 toString 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!