Last active
January 12, 2021 22:38
-
-
Save luisDVA/56dc340d7d0b2841c0c2c78f82440545 to your computer and use it in GitHub Desktop.
Empty rows and colums
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## %######################################################%## | |
# # | |
#### Empty rows and columns - your turn #### | |
# # | |
## %######################################################%## | |
# Import the Marine Protected Areas dataset (MPAS-your.csv) | |
# Identify the empty rows and columns, and create a new object with only the empty rows and columns | |
# Remove the empty rows and columns | |
# load packages ----------------------------------------------------------- | |
library(readr) | |
library(tibble) | |
library(dplyr) | |
library(naniar) | |
library(janitor) | |
library(unheadr) | |
# import data --------------------------------------------------------------- | |
MPAs <- read_csv("data/MPAS-your.csv") | |
# identify empty rows and columns ----------------------------------------- | |
# empty row ids | |
empty_rows <- MPAs %>% | |
rowid_to_column() %>% | |
filter(across(c(-rowid, -`Rank (by extent)`), is.na)) %>% | |
pull(rowid) | |
# empty column names | |
empty_cols <- MPAs %>% | |
select(where(all_na)) %>% | |
names() | |
# object with only empty rows and columns | |
MPAs %>% | |
rowid_to_column() %>% | |
select(rowid, empty_cols) %>% | |
slice(empty_rows) | |
# remove empty ------------------------------------------------------------ | |
MPAs %>% remove_empty(which = c("rows", "cols")) # still too many empty rows | |
MPAs %>% | |
remove_empty(which = c("rows", "cols")) %>% | |
mash_colnames(1) %>% | |
filter(!across(-`Rank (by extent)`, is.na)) # negate the row selection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment