I want to paste characters and separate them with certain sign, a , for example. Using paste() function works as expected:
paste("1st", "2nd", "3rd", sep = ", ")
[1] "1st, 2nd, 3rd"
But using the paste0() function results in a character with the separator only at the end:
paste0("1st", "2nd", "3rd", sep = ", ")
[1] "1st2nd3rd, "
I thought the main difference between both functions is that paste() makes a space by default and since I specified sep to be ", " in both examples I expect the same result. But this is not the case. Is this expected behaviour?
>Solution :
The paste0 function does not have a sep= parameter. It was specifically created to not use a separator. So these are all the same
paste0("1st", "2nd", "3rd", sep=", ")
paste0("1st", "2nd", "3rd", ", ")
paste0(a="1st", b="2nd", c="3rd", d=", ")
Check the ?paste0 help page for more info. If you want a separator, don’t use paste0. This is expected bahavior.