Tuesday, July 23, 2013

Comma separated vectors - R

> Months = c(1:24)


> paste(as.character(Months), collapse=", ")
[1] "1, 2, 3, 4, 5....24"

> paste(as.character(one), sep="' '", collapse=", ")
[1] "1, 2, 3, 4, 5....24"
What about if you want to go with quotes around the numbers then the “shQuote” function comes in handy and i’ve just learnt that.
Double quotes:
> cat(paste(shQuote(Months, type="cmd"), collapse=", "))
Single quotes:
> paste(shQuote(one), collapse=", ")


In addition, to output a csv file, which would be write.csv or write.table.
> write.table(matrix(as.character(Months),nrow=1), sep=",",
             row.names=FALSE, col.names=FALSE)


"1","2","3","4","5", …..,”24”

> write.csv(matrix(as.character(Months),nrow=1),row.names=FALSE)


"V1","V2","V3","V4","V5", ….”24”
"1","2","3","4","5", …...”24”

No comments:

Post a Comment

Add any comments if it helped :)