Pergunta

Many a times, I find myself typing the following

print(paste0(val1,',',val2,',',val3)) to print the output from a function with variables separated by a comma.

It is handy when I want to copy generate a csv file from the output.

I was wondering if I can write a function in R that does this for me. With many attempts, I could only get to this the following.

ppc <- function(string1,string2,...) print(paste0(string1,',',string2,',',...,))

It works well for at the maximum of three arguments.

> ppc(1,2,3)
[1] "1,2,3"
> ppc(1,2,3,4)
[1] "1,2,34" 

ppc(1,2,3,4) should have given "1,2,3,4". How can I correct my function? I somehow believe that this is possible in R.

Foi útil?

Solução

You don't need to write your own function. You can do this with paste.

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

Outras dicas

Or, in case you insist on a ppc() function:

ppc <- function(...) paste(...,sep=",")
ppc(1,2,3,4)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top