ComeçarComece de graça

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 exercício faz parte do curso

Intermediate Regular Expressions in R

Ver curso

Instruções do exercício

  • Print the column names of users by printing each column name separated by ", " in glue_collapse().
  • Use glue() and glue_collapse() to list the names of users stored in the name 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.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# 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 = "___")
)
Editar e executar o código