Import your data

dog <- read_excel("../01_module4/data/myData.xlsx") %>%
    janitor::clean_names()
set.seed(1234)
data_small <- dog %>%
    
    # Select three columns
    select(type, length, shedding_level ) %>%
    
    # Randomly select five rows
    sample_n(5) %>%
    slice(-2)

data_small
## # A tibble: 4 × 3
##   type   length shedding_level
##   <chr>  <chr>           <dbl>
## 1 Double Short               3
## 2 Double Long                1
## 3 Double Medium              3
## 4 Silky  Medium              3

Pivoting

long to wide form

data_small %>% pivot_wider(names_from = "length", values_from = "shedding_level")
## # A tibble: 2 × 4
##   type   Short  Long Medium
##   <chr>  <dbl> <dbl>  <dbl>
## 1 Double     3     1      3
## 2 Silky     NA    NA      3
data_wide <- data_small %>% pivot_wider(names_from = "length", values_from = "shedding_level")
data_wide
## # A tibble: 2 × 4
##   type   Short  Long Medium
##   <chr>  <dbl> <dbl>  <dbl>
## 1 Double     3     1      3
## 2 Silky     NA    NA      3

wide to long form

data_wide %>% pivot_longer(Short:Medium, names_to = "length", values_to = "shedding_level")
## # A tibble: 6 × 3
##   type   length shedding_level
##   <chr>  <chr>           <dbl>
## 1 Double Short               3
## 2 Double Long                1
## 3 Double Medium              3
## 4 Silky  Short              NA
## 5 Silky  Long               NA
## 6 Silky  Medium              3
data_wide %>% pivot_longer(Short:Medium, names_to = "length", values_to = "shedding_level", values_drop_na = TRUE)
## # A tibble: 4 × 3
##   type   length shedding_level
##   <chr>  <chr>           <dbl>
## 1 Double Short               3
## 2 Double Long                1
## 3 Double Medium              3
## 4 Silky  Medium              3

Separating and Uniting

Unite two columns

data_united <- dog %>%
    
    unite(col = "newName", affectionate_with_family:good_with_young_children, sep = "/", remove = TRUE)

Separate a column

data_united %>%
    
    separate(col = newName, into = c("affectionate_with_family", "good_with_young_children"), sep = "/")
## # A tibble: 195 × 18
##    column1 breed                   affectionate_with_fa…¹ good_with_young_chil…²
##      <dbl> <chr>                   <chr>                  <chr>                 
##  1       1 Retrievers (Labrador)   5                      5                     
##  2       2 French Bulldogs         5                      5                     
##  3       3 German Shepherd Dogs    5                      5                     
##  4       4 Retrievers (Golden)     5                      5                     
##  5       5 Bulldogs                4                      3                     
##  6       6 Poodles                 5                      5                     
##  7       7 Beagles                 3                      5                     
##  8       8 Rottweilers             5                      3                     
##  9       9 Pointers (German Short… 5                      5                     
## 10      10 Dachshunds              5                      3                     
## # ℹ 185 more rows
## # ℹ abbreviated names: ¹​affectionate_with_family, ²​good_with_young_children
## # ℹ 14 more variables: good_with_other_dogs <dbl>, shedding_level <dbl>,
## #   coat_grooming_frequency <dbl>, drooling_level <dbl>, type <chr>,
## #   length <chr>, openness_to_strangers <dbl>, playfulness_level <dbl>,
## #   watchdog_protective_nature <dbl>, adaptability_level <dbl>,
## #   trainability_level <dbl>, energy_level <dbl>, barking_level <dbl>, …

Missing Values