Skip to content

Instantly share code, notes, and snippets.

@AlbertRapp
Created February 18, 2023 14:18
Show Gist options
  • Save AlbertRapp/56e958acd0868940e0581adb29bf337e to your computer and use it in GitHub Desktop.
Save AlbertRapp/56e958acd0868940e0581adb29bf337e to your computer and use it in GitHub Desktop.
3 ways to relabel data
library(tidyverse)
big_tech_companies <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-02-07/big_tech_companies.csv')
big_tech_companies
# # A tibble: 14 × 2
# stock_symbol company
# <chr> <chr>
# 1 AAPL Apple Inc.
# 2 ADBE Adobe Inc.
# 3 AMZN Amazon.com, Inc.
# 4 CRM Salesforce, Inc.
# 5 CSCO Cisco Systems, Inc.
# 6 GOOGL Alphabet Inc.
# 7 IBM International Business Machines Corporat…
# 8 INTC Intel Corporation
# 9 META Meta Platforms, Inc.
# 10 MSFT Microsoft Corporation
# 11 NFLX Netflix, Inc.
# 12 NVDA NVIDIA Corporation
# 13 ORCL Oracle Corporation
# 14 TSLA Tesla, Inc.
## METHOD 1: case_when()
big_tech_companies |>
mutate(
company = case_when(
stock_symbol == 'AAPL' ~ "Apple",
stock_symbol == 'ADBE' ~ "Adobe",
stock_symbol == 'AMZN' ~ "Amazon",
stock_symbol == 'CRM' ~ "Salesforce",
stock_symbol == 'CSCO' ~ "Cisco Systems",
stock_symbol == 'GOOGL' ~ "Alphabet",
stock_symbol == 'IBM' ~ "IBM",
stock_symbol == 'INTC' ~ "Intel",
stock_symbol == 'META' ~ "Meta",
stock_symbol == 'MSFT' ~ "Microsoft",
stock_symbol == 'NFLX' ~ "Netflix",
stock_symbol == 'NVDA' ~ "NVIDIA",
stock_symbol == 'ORCL' ~ "Oracle",
stock_symbol == 'TSLA' ~ "Tesla",
)
)
# # A tibble: 14 × 2
# stock_symbol company
# <chr> <chr>
# 1 AAPL Apple
# 2 ADBE Adobe
# 3 AMZN Amazon
# 4 CRM Salesforce
# 5 CSCO Cisco Systems
# 6 GOOGL Alphabet
# 7 IBM IBM
# 8 INTC Intel
# 9 META Meta
# 10 MSFT Microsoft
# 11 NFLX Netflix
# 12 NVDA NVIDIA
# 13 ORCL Oracle
# 14 TSLA Tesla
## METHOD 2: case_match() saves a little bit of typing
big_tech_companies |>
mutate(
company = case_match(stock_symbol,
'AAPL' ~ "Apple",
'ADBE' ~ "Adobe",
'AMZN' ~ "Amazon",
'CRM' ~ "Salesforce",
'CSCO' ~ "Cisco Systems",
'GOOGL' ~ "Alphabet",
'IBM' ~ "IBM",
'INTC' ~ "Intel",
'META' ~ "Meta",
'MSFT' ~ "Microsoft",
'NFLX' ~ "Netflix",
'NVDA' ~ "NVIDIA",
'ORCL' ~ "Oracle",
'TSLA' ~ "Tesla",
)
)
## METHOD 3: if_else() + str_remove_all() + regex
big_tech_companies |>
mutate(
company = if_else(
stock_symbol == 'IBM',
'IBM',
company
),
company = str_remove_all(
company,
'( Platforms| Corporation| Inc\\.|\\.com|,)'
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment