typestar

Cleaning with tidyr in R

Splitting, uniting, and filling columns.

library(tidyr)
library(dplyr)

messy <- tibble(
  who = c("ada lovelace", "grace hopper"),
  score = c(95, NA)
)

tidy <- messy %>%
  separate(who, into = c("first", "last"), sep = " ") %>%
  replace_na(list(score = 0)) %>%
  unite("full_name", first, last, sep = " ", remove = FALSE)

complete_only <- drop_na(messy)

How it works

  1. separate splits one column into several.
  2. replace_na fills gaps per column.
  3. unite recombines columns; drop_na removes gaps.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
307
Tokens
91
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 55 seconds.

Type this snippet

Step 2 of 2 in Splitting columns, step 5 of 12 in Reshaping & reading.

← Previous Next →