ComenzarEmpieza gratis

Gluing data frames

Data is very often stored in data frames. Most of the time, we want to create an analysis that is also readable for humans. For example, it could be valuable to print a sentence about the size of our data frames. By combining glue() with nrow() and ncol(), we can return values and create a sentence that reports on the dimensions of our data frames.

Luckily, the glue package is part of the tidyverse package collection and was built with data frames in mind, so we can operate on entire data frame columns. We can, for example, use it inside mutate() to create a new column with a concatenated string that contains values of other columns. In this exercise, you will apply these examples on the users data frame which contains values of other columns.

Este ejercicio forma parte del curso

Intermediate Regular Expressions in R

Ver curso

Instrucciones del ejercicio

  • Use the glue() function to report the number of rows and columns users has, by storing them in the n and m temporary variables respectively.
  • Inspect the data frame users by just executing the line that prints the column names.
  • Mutate users to create a new column n_logins which reports the number of times users logged in by using the name and logins columns respectively.

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

# Create two temporary variables "n" and "m" and use them
glue(
  "The data frame 'users' has ___ rows and ___ columns.",
  ___ = nrow(users),
  ___ = ncol(users)
)

# This lists the column names of the data frame users
colnames(users)

# Use them to create a sentence about the numbers of logins
users %>% mutate(
  n_logins = glue("___ logged in ___ times.")
)
Editar y ejecutar código