ComenzarEmpieza gratis

Using replace_with_na scoped variants

To reduce code repetition when replacing values with NA, use the "scoped variants" of replace_with_na():

  • replace_with_na_at()
  • replace_with_na_if()
  • replace_with_na_all()

The syntax of replacement looks like this:

~.x == "N/A"

This replaces all cases that are equal to "N/A".

~.x %in% c("N/A", "missing", "na", " ")

Replaces all cases that have "N/A", "missing", "na", or " ".

Este ejercicio forma parte del curso

Dealing With Missing Data in R

Ver curso

Instrucciones del ejercicio

For the dataset pacman replace the same special missing values, "N/A", "missing", "na", and " ":

  • year, month, and day, using replace_with_na_at().
  • Only character variables using replace_with_na_if().
  • All variables using replace_with_na_all().

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# Use `replace_with_na_at()` to replace with NA
replace_with_na_at(pacman,
                   .vars = c(___, ___, ___), 
                   ~.x %in% c(___, ___, ___, ___))

# Use `replace_with_na_if()` to replace with NA the character values using `is.character`
replace_with_na_if(pacman,
                   .predicate = ___, 
                   ~.x %in% c(___, ___, ___, ___))

# Use `replace_with_na_all()` to replace with NA
replace_with_na_all(___, ___ %in% c(___, ___, ___, ___))
Editar y ejecutar código