Skip to content

Instantly share code, notes, and snippets.

@denisabd
Last active February 26, 2023 20:53
Show Gist options
  • Save denisabd/350e8ae4fbee95f998db5e101ea937ba to your computer and use it in GitHub Desktop.
Save denisabd/350e8ae4fbee95f998db5e101ea937ba to your computer and use it in GitHub Desktop.
R tidyverse useful tricks
library(tidyverse)
data <- tibble::tibble(
x = c(1, 2, NA, 2, NA),
y = c("a", NA, "b", NA, "b"),
z = c("x", NA, "y", NA, NA)
)
# Get counts of NA values for each column in a data frame
data %>%
purrr::map_df(~sum(is.na(.))) %>%
tidyr::pivot_longer(cols = everything()) %>%
dplyr::rename(column = name,
na_count = value) %>%
dplyr::arrange(-na_count)
# Remove rows with NA value present in any of the columns
data %>%
tidyr::drop_na()
# Replace NA values in all columns
data %>%
dplyr::mutate_all(~replace(., is.na(.), "N/A"))
# Replace NA values in all columns of the same data type, ex. is.character
# either mutate_if or mutate + across
data %>%
dplyr::mutate_if(is.character, ~tidyr::replace_na(., "N/A"))
data %>%
dplyr::mutate(
dplyr::across(where(is.character), \(x) tidyr::replace_na(x, "N/A"))
)
# Transform named list to tibble
my_list <- list(x = 5, y = 4, z = 12)
df <- my_list %>%
tibble::enframe() %>%
tidyr::unnest(cols = value)
# Create an empty data frame
df <- data.frame(
doubles = double(),
ints = integer(),
factors = factor(),
logicals = logical(),
characters = character(),
stringsAsFactors=FALSE
)
# Simple Stack Overflow Questions
# Drop column by name
data %>%
dplyr::select(-z, -y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment