3 Data Manipulation

oneway_data %>% 
  gather(key = "Type", value = "Sales", -Observation) %>% 
  # mutate(across(c(Observation,Type), ~as_factor(.x)))
  mutate(across(Observation:Type, ~as_factor(.x))) -> clean_oneway_data
clean_oneway_data
## # A tibble: 20 x 3
##    Observation Type      Sales
##    <fct>       <fct>     <dbl>
##  1 1           Colorless  26.5
##  2 2           Colorless  28.7
##  3 3           Colorless  25.1
##  4 4           Colorless  29.1
##  5 5           Colorless  27.2
##  6 1           Pink       31.2
##  7 2           Pink       28.3
##  8 3           Pink       30.8
##  9 4           Pink       27.9
## 10 5           Pink       29.6
## 11 1           Orange     27.9
## 12 2           Orange     25.1
## 13 3           Orange     28.5
## 14 4           Orange     24.2
## 15 5           Orange     26.5
## 16 1           Green      30.8
## 17 2           Green      29.6
## 18 3           Green      32.4
## 19 4           Green      31.7
## 20 5           Green      32.8
str(clean_oneway_data)
## tibble [20 x 3] (S3: tbl_df/tbl/data.frame)
##  $ Observation: Factor w/ 5 levels "1","2","3","4",..: 1 2 3 4 5 1 2 3 4 5 ...
##  $ Type       : Factor w/ 4 levels "Colorless","Pink",..: 1 1 1 1 1 2 2 2 2 2 ...
##  $ Sales      : num [1:20] 26.5 28.7 25.1 29.1 27.2 31.2 28.3 30.8 27.9 29.6 ...
clean_oneway_data %>% 
  sample_n(10)
## # A tibble: 10 x 3
##    Observation Type      Sales
##    <fct>       <fct>     <dbl>
##  1 1           Green      30.8
##  2 5           Green      32.8
##  3 3           Orange     28.5
##  4 3           Pink       30.8
##  5 2           Orange     25.1
##  6 5           Orange     26.5
##  7 2           Pink       28.3
##  8 3           Colorless  25.1
##  9 5           Pink       29.6
## 10 1           Orange     27.9
levels(clean_oneway_data$Type)
## [1] "Colorless" "Pink"      "Orange"    "Green"