Skip to content

Instantly share code, notes, and snippets.

@tanho63
Last active September 25, 2023 19:19
Show Gist options
  • Save tanho63/50d9b323e29165ad3e027bc3cf1c5926 to your computer and use it in GitHub Desktop.
Save tanho63/50d9b323e29165ad3e027bc3cf1c5926 to your computer and use it in GitHub Desktop.
pivot_longer sentinel .value - scoobydoo tidy tuesday
library(tidyverse)
scooby_data <- read.csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-07-13/scoobydoo.csv")
x <-
scooby_data %>%
select(season, title,
starts_with("caught"),
starts_with("captured"),
starts_with("unmask"),
starts_with("snack"),
-contains("other"),
-contains("not")) %>%
filter(title!= "Wrestle Maniacs")
three_lines <- x %>%
pivot_longer(cols = -c("season","title")) %>%
separate(name,into = c("action","character"), sep = "_") %>%
pivot_wider(names_from = "action", values_from = "value")
one_line <- x %>%
pivot_longer(cols = -c("season","title"), names_to = c(".value","character"), names_sep = "_") %>%
mutate(
across(c("caught","captured","unmask","snack"), ~as.logical(.x) %>% as.integer())
)
@IvoVillanueva
Copy link

Great

@jpcryne
Copy link

jpcryne commented May 24, 2023

Some of the best code I've ever seen

@jpcryne
Copy link

jpcryne commented Sep 25, 2023

It just gets better every time you see it

@IvoVillanueva
Copy link

IvoVillanueva commented Sep 25, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment