如何在矢量的每个值之间写一个逗号?

时间:2013-06-19 15:06:21

标签: r

我想将矢量导出到每个值之间带逗号的文件。我尝试将pastesep = ","一起使用,但它不起作用。有谁知道为什么?

> x <- rnorm(10)
> paste(x, sep = ",")
[1] "-1.08574649988891"  "2.9580381152357"    "-0.549880906960338" "1.79794352588269"   "-1.06316832291584"  "-1.56636513327118"  "0.363867572492577" 
[8] "-0.644707355221403" "0.617827074223129"  "-1.50892267338431" 

2 个答案:

答案 0 :(得分:8)

因为sep分隔了对象; collapse分隔元素。

> paste(letters[1:3], 1:3, sep=",", collapse="|")
[1] "a,1|b,2|c,3"

答案 1 :(得分:1)

试试这个:

> write.table(matrix(1:10, 1), sep = ",", row.names = FALSE, col.names = FALSE)
1,2,3,4,5,6,7,8,9,10

或者这个:

> cat(1:10, sep = ","); cat("\n")
1,2,3,4,5,6,7,8,9,10

另请注意,write.tablecat都支持file=append=个参数。