Quantcast
Channel: TRinker's R Blog » paste
Viewing all articles
Browse latest Browse all 4

paste, paste0, and sprintf

0
0

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 using paste (That is whether I 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:

  1. A bunch of individual character strings.
  2. 2 or more strings pasted element for element.
  3. 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:

  1. A bunch of individual character strings – You want sep
  2. 2 or more strings pasted element for element. – You want sep
  3. One string smushed together.- Smushin requires collapse

So here they are with the correct arguments:

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, collapse="") #One string smushed together.

This yields:

> paste("A", 1, "%")       #A bunch of individual character strings.
[1] "A 1 %"
> paste(1:4, letters[1:4]) #2 or more strings pasted element for element.
[1] "1 a" "2 b" "3 c" "4 d"
> paste(1:10, collapse="") #One string smushed together.
[1] "12345678910"

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



Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images