Import your data

haunted <- read.csv("../00_data/MyData.csv")
set.seed(1234)
data <- haunted %>%
    select(County, location, longitude) %>%
    sample_n(5)

data
##      County                  location longitude
## 1   Belding           Belding Library -85.22682
## 2 Coldwater            Halstead House        NA
## 3  Bay City old water treatment plant -83.87410
## 4   Allegan                Elks Lodge -85.84160
## 5    Albion            Albion College -84.74518

Pivoting

long to wide form

data_wide <- data %>%
    pivot_wider(names_from = "location",
                values_from = "longitude")

data_wide
## # A tibble: 5 × 6
##   County  `Belding Library` `Halstead House` old water treatment …¹ `Elks Lodge`
##   <chr>               <dbl>            <dbl>                  <dbl>        <dbl>
## 1 Belding             -85.2               NA                   NA           NA  
## 2 Coldwa…              NA                 NA                   NA           NA  
## 3 Bay Ci…              NA                 NA                  -83.9         NA  
## 4 Allegan              NA                 NA                   NA          -85.8
## 5 Albion               NA                 NA                   NA           NA  
## # ℹ abbreviated name: ¹​`old water treatment plant`
## # ℹ 1 more variable: `Albion College` <dbl>

wide to long form

data_wide %>%
    pivot_longer(cols = `Belding Library`:`Albion College`, values_drop_na = TRUE, 
                 names_to = "location", values_to = "longitude")
## # A tibble: 4 × 3
##   County   location                  longitude
##   <chr>    <chr>                         <dbl>
## 1 Belding  Belding Library               -85.2
## 2 Bay City old water treatment plant     -83.9
## 3 Allegan  Elks Lodge                    -85.8
## 4 Albion   Albion College                -84.7

Separating and Uniting

Separate a column

table3_sep <- table3 %>%
    
    separate(col = rate, into = c("location", "longitude"))

Unite two columns

table3_sep %>%
    
        unite(col = rate, c("location","longitude"), sep = "/", )
## # A tibble: 6 × 3
##   country      year rate             
##   <chr>       <dbl> <chr>            
## 1 Afghanistan  1999 745/19987071     
## 2 Afghanistan  2000 2666/20595360    
## 3 Brazil       1999 37737/172006362  
## 4 Brazil       2000 80488/174504898  
## 5 China        1999 212258/1272915272
## 6 China        2000 213766/1280428583

Missing Values