r4ds-exercise-solutions icon indicating copy to clipboard operation
r4ds-exercise-solutions copied to clipboard

An addition to the solution for Exercise 14.2.1

Open AnttiRask opened this issue 2 years ago • 1 comments

This is all well and good:

"Since str_c() does not separate strings with spaces by default it is closer in behavior to paste0()."

str_c("foo", "bar")
#> [1] "foobar"

But after that I would add something like this:

"However, with the use of the "sep" argument, str_c can replace both paste and paste0.

str_c("foo", "bar", sep = " ")
#> [1] "foo bar"

Link to the page: https://jrnold.github.io/r4ds-exercise-solutions/strings.html#exercise-14.2.1

Thank you!

AnttiRask avatar Apr 30 '22 11:04 AnttiRask

Also, if you really want, you can get the same results for these ones by using str_replace_na(NA).

str_c("foo", str_replace_na(NA), sep = " ")
#> [1] "foo NA"

instead of

paste("foo", NA)
#> [1] "foo NA"

and

str_c("foo", str_replace_na(NA))
#> [1] "fooNA"

instead of

paste0("foo", NA)
#> [1] "fooNA"

AnttiRask avatar Apr 30 '22 11:04 AnttiRask