Collapsing multiple elements into a string

Intermediate Regular Expressions in R

Angelo Zehr

Data Journalist

Introducing glue_collapse

usernames <- c("Adam", "Betty", "Cora", "David")

glue_collapse(usernames)

Will print AdamBettyCoraDavid

Intermediate Regular Expressions in R

More elaborate example

glue_collapse(
  usernames,
  sep = ", ",
  last = ", and ",
  width = 27
)

Will print Adam, Betty, Cora, and Dav...

Intermediate Regular Expressions in R

Passing glue collapse to glue

glue(
  "Hello {users}.",
  users = glue_collapse(
    usernames,
    sep = ", ",
    last = ", and "
  )
)

Will print Hello Adam, Betty, Cora, and David.

Intermediate Regular Expressions in R

Collapsing columns of a data frame

Data frame df

  x  y
  1  4
  2  5
  3  6

glue_collapse(df$x) will print 123

Intermediate Regular Expressions in R

Let's practice!

Intermediate Regular Expressions in R

Preparing Video For Download...