paste, paste0, and sprintf
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I find myself pasting urls and lots of little pieces together lately. Now paste is a standard go to guy when you wanna glue some stuff together. But often I find myself pasting and getting stuff like this:
paste(LETTERS) [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" [18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
Rather than the desired…
[1] "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
When I get into those situations I think oh better use collapse instead but never really think before what I need (collapse
or sep
and why. This is inefficient and causes me to lack the time to write quality articles for Fox News (JK for those taking me serious). This tutorial will give some basic and clear direction about the following functions:
paste(x) paste0(x) sprintf(x, y)
paste
paste has 3 arguments.
paste (..., sep = " ", collapse = NULL)
The ...
is the stuff you want to paste together and sep
and collapse
are the guys to get it done. There are three basic things I paste together:
- A bunch of individual character strings.
- 2 or more strings pasted element for element.
- One string smushed together.
Here's an example of each, though not with the correct arguments (I'm building suspense here):
paste("A", 1, "%") #A bunch of individual character strings. paste(1:4, letters[1:4]) #2 or more strings pasted element for element. paste(1:10) #One string smushed together.
Here's the sep
/collapse
rule for each:
- A bunch of individual character strings – You want sep
- 2 or more strings pasted element for element. – You want sep
- One string smushed together.- Smushin requires collapse
paste0
paste0 is short for:
paste(x, sep="")
So it allows us to be lazier and more efficient. I'm lazy so I use paste0 a lot.
paste0("a", "b") == paste("a", "b", sep="") ## [1] TRUE
'nuff said.
sprintf
I discovered this guy a while back but realized it's value in pasting recently. Much of my work on the reports (Rinker, 2013) package requires that I pieces together lots of chunks of url and insert user specific pieces. This can be a nightmare with all the quotation marks. A typical take may look like this:
person <-"Grover" action <-"flying" message(paste0("On ", Sys.Date(), " I realized ", person, " was...\n", action, " by the street")) ## On 2013-09-14 I realized Grover was... flying by the street
No joke it took me 6 tries before I formatted that without an error (missing quotes, spaces, and commas).
But we can use sprintf to make one string (less commas + less quotations marks = less errors) and feed the elements that may differ from user to user or time to time. Let's look at an example to see what I mean:
person <-"Grover" action <-"flying" message(sprintf("On %s I realized %s was...\n%s by the street", Sys.Date(), person, action)) ## On 2013-09-14 I realized Grover was... flying by the street
Boom first time. It's easy to figure out the spacing and there aren't the commas and quotation marks to deal with. Just use the %s
marker to denote that some element goes here and then feed it in as a vector after the character string. For some applications sprintf is a superior choice over paste
/paste0
.
Note that these are not extensive, all-encompassing rules but guides for general use. Also be aware the sprintf
is even cooler than I demonstrated here.
*Created using the reports package
References
- Tyler Rinker, (2013) reports: Package to assist in report writing. http://github.com/trinker/reports
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.