Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created June 3, 2020 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njtierney/47f2beb12a74b35f55d0ffbde55700dc to your computer and use it in GitHub Desktop.
Save njtierney/47f2beb12a74b35f55d0ffbde55700dc to your computer and use it in GitHub Desktop.
library(tidyverse)
df_console <- tibble(
  name = c("Smash Bros.", "Red Dead", "Spiderman 4", "Overcooked", "Zelda"),
  sales = c(2001:2005),
  platform = c("NS",
               "PS4",
               "PS4)(PS4",
               "NS(2017)",
               "XboxOne")
)

# identify strange cases
df_console %>% 
  count(platform)
#> # A tibble: 5 x 2
#>   platform     n
#>   <chr>    <int>
#> 1 NS           1
#> 2 NS(2017)     1
#> 3 PS4          1
#> 4 PS4)(PS4     1
#> 5 XboxOne      1

df_console %>% 
  mutate(platform_tidy = case_when(
    platform == "NS(2017)" ~ "NS",
    platform == "PS4)(PS4" ~ "PS4",
    TRUE ~ platform
  ))
#> # A tibble: 5 x 4
#>   name        sales platform platform_tidy
#>   <chr>       <int> <chr>    <chr>        
#> 1 Smash Bros.  2001 NS       NS           
#> 2 Red Dead     2002 PS4      PS4          
#> 3 Spiderman 4  2003 PS4)(PS4 PS4          
#> 4 Overcooked   2004 NS(2017) NS           
#> 5 Zelda        2005 XboxOne  XboxOne

Created on 2020-06-03 by the reprex package (v0.3.0)

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