Clean / Drop"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(dplyr)
library(explore)

Rename variable

data <- use_data_titanic(count = FALSE)
glimpse(data)
data <- data %>% clean_var(Age, name = "age")
glimpse(data)

Replace NA values

data <- use_data_beer()
data %>% describe(energy_kcal_100ml)
data <- data %>% clean_var(energy_kcal_100ml, na = 42)
data %>% describe(energy_kcal_100ml)

Set min max values

data <- create_data_person()
data %>% describe(age)
data <- data %>% clean_var(age, min_val = 20, max_val = 80)
data %>% describe(age)

Rescale 0 to 1

data %>% describe(income)
data <- data %>% clean_var(income, rescale01 = TRUE)
data %>% describe(income)

Cleaning text

data[1, "handset"] <- " android "
data[2, "handset"] <- "ANDROID"
data %>% describe(handset)
data <- data %>% clean_var(handset, simplify_text = TRUE)
data %>% describe(handset)

Drop variables

data <- use_data_beer()
data %>% describe_tbl()
data %>%
  drop_var_no_variance() %>%
  describe_tbl()
data %>%
  drop_var_with_na() %>%
  describe_tbl()

Drop observations

data %>%
  drop_obs_with_na() %>%
  describe_tbl()
data %>%
  count_pct(type)
data %>%
  drop_obs_if(type == "Alkoholfrei") %>%
  count_pct(type)


Try the explore package in your browser

Any scripts or data that you put into this service are public.

explore documentation built on Sept. 11, 2024, 7:40 p.m.