折叠字符向量时如何避免空格?

时间:2017-10-13 06:45:38

标签: r

我有一个字符向量,想要一个像这样的输出:

"(WLNXL0427M)|(QWDAF7505E)|(ZTKQC4319H)|(RUQAI0874B)|(BAUPR0486O)"

我用

v <- c("WLNXL0427M", "QWDAF7505E", "ZTKQC4319H", "RUQAI0874B", "BAUPR0486O")
a <- paste("(", v, collapse = "|", ")")
a

得到

"( WLNXL0427M )|( QWDAF7505E )|( ZTKQC4319H )|( RUQAI0874B )|( BAUPR0486O )"

如何避免空格?

1 个答案:

答案 0 :(得分:3)

也可以使用paste0(),即

v <- c("WLNXL0427M", "QWDAF7505E", "ZTKQC4319H", "RUQAI0874B", "BAUPR0486O")
a <- paste0("(", v, collapse = "|", ")")
a