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.
Este ejercicio forma parte del curso
Intermediate Regular Expressions in R
Instrucciones del ejercicio
- 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.
Ejercicio interactivo práctico
Prueba este ejercicio completando el código de muestra.
# 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 = "___")
)