Collapsing data frames
In the last exercise you already combined glue()
and glue_collapse()
to create a correct English sentence from a vector. But very often, you will work not with vectors but with data frames. Luckily the workflow for data frame columns is the same as it is for vectors.
In the scope you have again our data frame users
with three names and numbers of logins in it. Use glue_collapse()
and print the columns of the data frame in a human readable form.
This exercise is part of the course
Intermediate Regular Expressions in R
Exercise instructions
- Print the column names of
users
by printing each column name separated by", "
inglue_collapse()
. - Use
glue()
andglue_collapse()
to list the names of users stored in thename
column in a human-readable way, while ensuring the last name is preceded by" and "
. - Repeat the same steps, this time for the number of logins stored in the
logins
column.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# List colnames separated a comma and a white space
glue_collapse(colnames(users), sep = "___")
# Use " and " for the last elements in glue_collapse
glue(
"Our users are called {names}.",
names = ___(users$___, sep = "___", last = "___")
)
# Use the same way to output also the "logins" of the users
glue(
"Our users have logged in {logins} times.",
logins = ___(users$___, sep = "___", last = "___")
)