Import your data

my_data <- read_excel("../00_data/myData_all.xlsx")
my_data
## # A tibble: 220 × 7
##    observed_month      prod_type  prod_process n_hens n_eggs source Eggs_Per_Hen
##    <dttm>              <chr>      <chr>         <dbl>  <dbl> <chr>         <dbl>
##  1 2016-07-31 00:00:00 hatching … all          5.80e7 1.15e9 ChicE…         19.8
##  2 2016-08-31 00:00:00 hatching … all          5.76e7 1.14e9 ChicE…         19.8
##  3 2016-09-30 00:00:00 hatching … all          5.72e7 1.09e9 ChicE…         19.1
##  4 2016-10-31 00:00:00 hatching … all          5.69e7 1.13e9 ChicE…         19.8
##  5 2016-11-30 00:00:00 hatching … all          5.71e7 1.10e9 ChicE…         19.2
##  6 2016-12-31 00:00:00 hatching … all          5.77e7 1.13e9 ChicE…         19.6
##  7 2017-01-31 00:00:00 hatching … all          5.80e7 1.12e9 ChicE…         19.4
##  8 2017-02-28 00:00:00 hatching … all          5.83e7 1.01e9 ChicE…         17.4
##  9 2017-03-31 00:00:00 hatching … all          5.87e7 1.13e9 ChicE…         19.2
## 10 2017-04-30 00:00:00 hatching … all          5.91e7 1.10e9 ChicE…         18.6
## # ℹ 210 more rows

Make data small

my_data_small <- my_data %>% select(observed_month, n_hens, n_eggs) %>% sample_n(10)
my_data_small
## # A tibble: 10 × 3
##    observed_month        n_hens      n_eggs
##    <dttm>                 <dbl>       <dbl>
##  1 2018-05-31 00:00:00 15559000  361746617.
##  2 2020-01-31 00:00:00 15651500  368748720 
##  3 2017-09-30 00:00:00 60202000 1120900000 
##  4 2019-12-31 00:00:00 16226500  383553257.
##  5 2019-04-30 00:00:00 62900000 1165600000 
##  6 2017-07-31 00:00:00 27500000  643637057.
##  7 2018-06-30 00:00:00 62428000 1161400000 
##  8 2018-10-31 00:00:00 60889000 1184200000 
##  9 2016-09-30 00:00:00 57161000 1093300000 
## 10 2018-02-28 00:00:00 14996000  313866720

Pivoting

long to wide form

my_data_small_long <- my_data_small %>%
    
    pivot_longer(cols = c('n_hens', 'n_eggs'), 
                          names_to = "data_type", 
                          values_to = "number_prod")
my_data_small_long
## # A tibble: 20 × 3
##    observed_month      data_type number_prod
##    <dttm>              <chr>           <dbl>
##  1 2018-05-31 00:00:00 n_hens      15559000 
##  2 2018-05-31 00:00:00 n_eggs     361746617.
##  3 2020-01-31 00:00:00 n_hens      15651500 
##  4 2020-01-31 00:00:00 n_eggs     368748720 
##  5 2017-09-30 00:00:00 n_hens      60202000 
##  6 2017-09-30 00:00:00 n_eggs    1120900000 
##  7 2019-12-31 00:00:00 n_hens      16226500 
##  8 2019-12-31 00:00:00 n_eggs     383553257.
##  9 2019-04-30 00:00:00 n_hens      62900000 
## 10 2019-04-30 00:00:00 n_eggs    1165600000 
## 11 2017-07-31 00:00:00 n_hens      27500000 
## 12 2017-07-31 00:00:00 n_eggs     643637057.
## 13 2018-06-30 00:00:00 n_hens      62428000 
## 14 2018-06-30 00:00:00 n_eggs    1161400000 
## 15 2018-10-31 00:00:00 n_hens      60889000 
## 16 2018-10-31 00:00:00 n_eggs    1184200000 
## 17 2016-09-30 00:00:00 n_hens      57161000 
## 18 2016-09-30 00:00:00 n_eggs    1093300000 
## 19 2018-02-28 00:00:00 n_hens      14996000 
## 20 2018-02-28 00:00:00 n_eggs     313866720

wide to long form

my_data_small_long %>%
    
    pivot_wider(names_from = data_type,
                values_from = number_prod)
## # A tibble: 10 × 3
##    observed_month        n_hens      n_eggs
##    <dttm>                 <dbl>       <dbl>
##  1 2018-05-31 00:00:00 15559000  361746617.
##  2 2020-01-31 00:00:00 15651500  368748720 
##  3 2017-09-30 00:00:00 60202000 1120900000 
##  4 2019-12-31 00:00:00 16226500  383553257.
##  5 2019-04-30 00:00:00 62900000 1165600000 
##  6 2017-07-31 00:00:00 27500000  643637057.
##  7 2018-06-30 00:00:00 62428000 1161400000 
##  8 2018-10-31 00:00:00 60889000 1184200000 
##  9 2016-09-30 00:00:00 57161000 1093300000 
## 10 2018-02-28 00:00:00 14996000  313866720

Separating and Uniting

Unite two columns

my_data_small_unite <- my_data_small %>%
    
    unite(col = "rate", c(n_eggs, n_hens), sep = "/", )

my_data_small_unite
## # A tibble: 10 × 2
##    observed_month      rate                     
##    <dttm>              <chr>                    
##  1 2018-05-31 00:00:00 361746617.142857/15559000
##  2 2020-01-31 00:00:00 368748720/15651500       
##  3 2017-09-30 00:00:00 1120900000/60202000      
##  4 2019-12-31 00:00:00 383553257.142857/16226500
##  5 2019-04-30 00:00:00 1165600000/62900000      
##  6 2017-07-31 00:00:00 643637057.142857/27500000
##  7 2018-06-30 00:00:00 1161400000/62428000      
##  8 2018-10-31 00:00:00 1184200000/60889000      
##  9 2016-09-30 00:00:00 1093300000/57161000      
## 10 2018-02-28 00:00:00 313866720/14996000

Separate a column

my_data_small_sep <- my_data_small_unite %>%
    
    separate(col = rate, into = c("n_eggs", "n_hens"))
## Warning: Expected 2 pieces. Additional pieces discarded in 3 rows [1, 4, 6].
my_data_small_sep
## # A tibble: 10 × 3
##    observed_month      n_eggs     n_hens  
##    <dttm>              <chr>      <chr>   
##  1 2018-05-31 00:00:00 361746617  142857  
##  2 2020-01-31 00:00:00 368748720  15651500
##  3 2017-09-30 00:00:00 1120900000 60202000
##  4 2019-12-31 00:00:00 383553257  142857  
##  5 2019-04-30 00:00:00 1165600000 62900000
##  6 2017-07-31 00:00:00 643637057  142857  
##  7 2018-06-30 00:00:00 1161400000 62428000
##  8 2018-10-31 00:00:00 1184200000 60889000
##  9 2016-09-30 00:00:00 1093300000 57161000
## 10 2018-02-28 00:00:00 313866720  14996000

Missing Values