Skip to content

Instantly share code, notes, and snippets.

@briandk
Created November 27, 2014 07:11
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save briandk/d9231ba1e2603eed0df1 to your computer and use it in GitHub Desktop.
Save briandk/d9231ba1e2603eed0df1 to your computer and use it in GitHub Desktop.
Understanding `sep` and `collapse` in R using `paste()
# The difference between the `sep` and `collapse` arguments
# in paste can be thought of like this:
#
# paste can accept multiple *vectors* as input, and will
# concatenate the ith entries of each vector pairwise
# (or tuplewise), if it can.
#
# When you pass paste multiple vectors, sep defines what
# separates the entries in those tuple-wise concatenations.
#
# When you pass paste a *collapse* value, it will return
# any concatenated pairs as part of a single length-1
# character vector, with the tuples separated by
# the string you passed to `collapse`
x <- c("a", "b", "c", "d")
y <- c("w", "x", "y", "z")
# The statement below will return
# "a%%w" "b%%x" "c%%y" "d%%z"
# A tuple of character concatenations, where within
# each tuple, the concatenations are separated by the sep
# argument
paste(x, y, sep="%%")
# The statement below this will return
# "a%%w,b%%x,c%%y,d%%z"
# It's the same tuple-wise concatenation as above,
# but this time there's the additional step of reducing
# the output to a vector of length one and shoving the
# collapse value between the entries. Note how in the output
# there are four instances of "%%", but only three of ","
paste(x, y, sep="%%", collapse=",")
# To confirm our conjecture, we can swap the sep and
# collapse arguments. The statement below will return
# "a,w%%b,x%%c,y%%d,z"
paste(x, y, sep=",", collapse="%%")
@davidrab
Copy link

Thanks

@MichaelSzczepaniak
Copy link

This bugged me for quite awhile. Thanks for the very good explanation in your comments.

@Ebenezer2016
Copy link

Thanks

@alihammadbaig
Copy link

Clean example. Thanks

@lornamariak
Copy link

thanks!much understandable

@tigerfanxiao
Copy link

Thanks a lot, after read severals time, finally I understood.

@jialinhuang00
Copy link

that's really clear! thx!

@adeel2abdullah
Copy link

nice and clear. thanks.

@Deepgitvault
Copy link

That's well put!

@muye0928
Copy link

thanks

@Durga32670
Copy link

while(1){
print("Thanks")
}

@thebjko
Copy link

thebjko commented Nov 2, 2018

Thank you!

@omni-
Copy link

omni- commented Jan 30, 2019

Great work, thanks,

@hack-r
Copy link

hack-r commented Mar 26, 2019

This is great and works for me, tho for others I think explaining %% when you introduce that notation would be useful

@iamloivx
Copy link

Thank you!

@jaamarks
Copy link

Much obliged

@jayant-gupta
Copy link

thanks

@bj20ug19
Copy link

Awesome! Example at the end seals it. thanks!

@hammao
Copy link

hammao commented Sep 28, 2020

Wow.... Thanks !!!

@Slow-LearnR
Copy link

Thank you!

@sasank89
Copy link

Thank you!

@jxshen311
Copy link

Thank you! very informative!

@Anant-mishra1729
Copy link

Anant-mishra1729 commented Dec 4, 2022

Nice explanation, to add further, try running this

length(paste(x,y,sep = "%%",collapse=","))

Output: 1 (Returns a single character)
and this

length(paste(x,y,sep = "%%"))

Output: 4 (Returns character with length 4)

@hammao
Copy link

hammao commented Jan 20, 2023

@Anant-mishra1729, you may need to edit the second code without the "collapse" to give the expected result, for the sake of new R users length(paste(x,y,sep = "%%"))

@Divyam6969
Copy link

aah thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment