2

I want to paste a number of arguments which vary according to a parameter, with a comma separator "," between them. For instance:

pred.size <- 2
paste(c(1:pred.size), sep=",")

results in:

##"1" "2"

while the result I want is:

##"1","2"
3
  • Do you mean the result you want is "1,2" as a single string? Commented May 13, 2014 at 20:34
  • Try paste(c(1:pred.size), collapse=",") or paste(seq_len(pred.size), collapse=",") Commented May 13, 2014 at 20:44
  • If you really want the quotes in the string, try: paste(1:pred.size,collapse="\",\"") Commented May 13, 2014 at 21:39

1 Answer 1

2

I think you want to paste together the elements of a vector like 1:2 to get a comma separated string. To do this you use the collapse argument of paste, since you are passing it only a single argument.

paste(1:3, collapse = ",")
[1] "1,2,3"

On the other hand if you passed multiple terms you would use sep:

paste(1, 2, 3, sep = ",")
[1] "1,2,3"

sep separates the arguments, collapse separates the components of vector arguments. For example:

paste(1:4, 5:8, collapse=",", sep="|")
[1] "1|5,2|6,3|7,4|8"

Type ?paste at the R prompt for more information.

So you want

paste(1:pred.size, collapse=",") 

Your c is not necessary because 1:pred_size is already a vector.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.