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
separatesplits one column into several.replace_nafills gaps per column.uniterecombines columns;drop_naremoves gaps.
Keywords and builtins used here
cdrop_nalibrarylistreplace_naseparatetibbleunite
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.
Step 2 of 2 in Splitting columns, step 5 of 12 in Reshaping & reading.