13

I don't seem to see a difference between paste/paste0 and str_c for combining a single vector into a single string, multiple strings into one string, or multiple vectors into a single string.

While I was writing the question I found this: https://www.rdocumentation.org/packages/stringr/versions/1.3.1/topics/str_c. The community example from richie@datacamp.com says the difference is is that str_c treats blanks as blanks (not as NAs) and recycles more appropriately. Any other differences?

Ben G
  • 4,148
  • 2
  • 22
  • 42
  • 2
    One difference is that `str_c` is faster. – IceCreamToucan Jun 12 '18 at 14:12
  • 1
    Though the speed difference seems to be almost negligible. Benchmarking `paste0` vs `str_c` with inputs `letters, letters`, the time difference is about 30%, but that 30% is 2.1 microseconds on my computer, or 0.0000021 seconds. It would be strange code where `paste` is the bottleneck... – Gregor Thomas Jun 12 '18 at 14:23
  • https://stackoverflow.com/questions/36279800/difference-between-paste-and-paste0 – user5249203 Jun 12 '18 at 14:27
  • @user5249203, I get the difference between `paste` and `paste0`, I'm trying to figure out how these differ from `str_c` – Ben G Jun 12 '18 at 19:02
  • 1
    relevant : https://stackoverflow.com/questions/53118271/difference-between-paste-str-c-str-join-stri-join-stri-c-stri-pa/53118273#53118273 – moodymudskipper Nov 02 '18 at 12:38

2 Answers2

15

paste0(..., collapse = NULL)is a wrapper for paste(..., sep = "", collapse = NULL), which means there is no separator. In other words, with paste0() you can not apply some sort of separator, while you do have that option with paste(), whereas a single space is the default.

str_c(..., sep = "", collapse = NULL) is equivalent to paste(), which means you do have the option to customize your desired separator. The difference is for str_c() the default is no separator, so it acts just like paste0() as a default.

Paste() and paste0() are both functions from the base package, whereas str_c() comes from the stringr package.

I did not test/microbenchmark it, but from my experience I do agree to Ryan str_c() is generally faster.

Lennyy
  • 5,932
  • 2
  • 10
  • 23
1

From https://campus.datacamp.com/courses/string-manipulation-with-stringr-in-r/introduction-to-stringr?ex=2

There are two key ways str_c() differs from paste(). First, the default separator is an empty string, sep = "", as opposed to a space, so it's more like paste0(). This is an example of a stringr function, performing a similar operation to a base function, but using a default that is more likely to be what you want. {...}

The second way str_c() differs to paste() is in its handling of missing values. paste() turns missing values into the string "NA", whereas str_c() propagates missing values. That means combining any strings with a missing value will result in another missing value.

vrognas
  • 33
  • 6