0
votes

I have a list here.

NewList <- list(a="abc",b="xyz",c="lmn")

If I run Reduce(f = function(x,y){paste0(x,y,sep=";")},x=NewList) surprisingly, it gives me "abcxyz;lmn;"

If I run Reduce(f = function(x,y){paste(x,y,sep=";")},x=NewList), the answer is as expected "abc;xyz;lmn".

Can anyone help me explain why paste0 gives the results above different from paste?

1

1 Answers

1
votes

According to ?paste

paste0(..., collapse) is equivalent to paste(..., sep = "", collapse), slightly more efficiently.

By providing a new sep, it is creating a conflict with the already existing sep. Instead use paste with sep


Here, we can directly use paste without any Reduce as the length of each of the elements of list is 1

paste(NewList, collapse=";")
#[1] "abc;xyz;lmn"

Or

paste0(NewList, collapse=";")
#[1] "abc;xyz;lmn"

Note that in the above case, we are not touching the sep