对于 a data.frame
,您可能想尝试:
> do.call(paste0, df)
[1] "A1" "B2" "C3" "D4" "E5"
或者,如果您想要一个带逗号的字符串,请按如下方式修改您的方法:
> paste0(df$letters, df$numbers, collapse = ", ")
[1] "A1, B2, C3, D4, E5"
在那里获取报价具有欺骗性:
> paste0('"', paste0(df$letters, df$numbers, collapse = '", "'), '"')
[1] "\"A1\", \"B2\", \"C3\", \"D4\", \"E5\""
看起来有反斜杠,但没有:
> cat(paste0('"', paste0(df$letters, df$numbers, collapse = '", "'), '"'))
"A1", "B2", "C3", "D4", "E5"
> nchar(paste0('"', paste0(df$letters, df$numbers, collapse = '", "'), '"'))
[1] 28
或者,也许您正在寻找dQuote
,sQuote
或者shQuote
(很难说不知道为什么要做您正在做的事情):
> noquote(paste(dQuote(paste0(df$letters, df$numbers)), collapse = ", "))
[1] “A1”, “B2”, “C3”, “D4”, “E5”
> noquote(paste(shQuote(paste0(df$letters, df$numbers)), collapse = ", "))
[1] 'A1', 'B2', 'C3', 'D4', 'E5'
> noquote(paste(sQuote(paste0(df$letters, df$numbers)), collapse = ", "))
[1] ‘A1’, ‘B2’, ‘C3’, ‘D4’, ‘E5’