开发者

How to insert a comma between each element in paste command in R?

开发者 https://www.devze.com 2023-01-29 22:14 出处:网络
How can I insert a comma between each element in paste command in R ? paste (\"X\",1:5,sep=\"\") \"X1\" \"X2\" \"X3\" \"X4\" \"X5\"

How can I insert a comma between each element in paste command in R ?

paste ("X",1:5,sep="")

"X1" "X2" "X3" "X4" "X5"

Now I want to insert a comma between each element

Desired Output 

"X1","X2","X3","X4","X5"
开发者_运维百科

Thanks for your help


I think one of the two commands below should work for you:

> paste ("X",1:5,sep="", collapse=",")
[1] "X1,X2,X3,X4,X5"
> paste ("'","X",1:5,"'",sep="", collapse=",")
[1] "'X1','X2','X3','X4','X5'"

Update, based on comments:

There's no need to put commas "between" the vector elements. You can use the output of your paste command as the col.names arg to read.table.

lines <-
"0 1 2 3 4
 5 6 7 8 9"

con <- textConnection(lines)
cnames <- paste("X",1:5,sep="")
x <- read.table(con, col.names=cnames)
close(con)
x
#   X1 X2 X3 X4 X5
# 1  0  1  2  3  4
# 2  5  6  7  8  9
0

精彩评论

暂无评论...
验证码 换一张
取 消