Challenge 3

Author

Jaden Busch

Read in data

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
dataset <- read.csv("../challenge_datasets/animal_weight.csv")
dataset
            IPCC.Area Cattle...dairy Cattle...non.dairy Buffaloes
1 Indian Subcontinent            275                110       295
2      Eastern Europe            550                391       380
3              Africa            275                173       380
4             Oceania            500                330       380
5      Western Europe            600                420       380
6       Latin America            400                305       380
7                Asia            350                391       380
8         Middle east            275                173       380
9    Northern America            604                389       380
  Swine...market Swine...breeding Chicken...Broilers Chicken...Layers Ducks
1             28               28                0.9              1.8   2.7
2             50              180                0.9              1.8   2.7
3             28               28                0.9              1.8   2.7
4             45              180                0.9              1.8   2.7
5             50              198                0.9              1.8   2.7
6             28               28                0.9              1.8   2.7
7             50              180                0.9              1.8   2.7
8             28               28                0.9              1.8   2.7
9             46              198                0.9              1.8   2.7
  Turkeys Sheep Goats Horses Asses Mules Camels Llamas
1     6.8  28.0  30.0    238   130   130    217    217
2     6.8  48.5  38.5    377   130   130    217    217
3     6.8  28.0  30.0    238   130   130    217    217
4     6.8  48.5  38.5    377   130   130    217    217
5     6.8  48.5  38.5    377   130   130    217    217
6     6.8  28.0  30.0    238   130   130    217    217
7     6.8  48.5  38.5    377   130   130    217    217
8     6.8  28.0  30.0    238   130   130    217    217
9     6.8  48.5  38.5    377   130   130    217    217

Briefly describe the data

The data appears to represent the typical weight of a given animal across different areas of the world. The first column represents the area of the world that animal is from via a string, and all other columns represent the typical weight of the animal given in the column name via an int.

This data is not that easy to work with, because each row does not correspond to a single observance of an animal from a location, but rather each row corresponds to all the animals from a given area. To make it tidy, I will make it so that each row corresponds to a single weight for a given animal-location pair.

Anticipate the End Result

We want the name of very column except the “IPCC.Area” to be contained in a new column named called “animal_name”. Then, the corresponding values should go into a new column named “weight”. By performing this operation, our new dataframe should have three total columns: IPCC.Area, animal_name, and weight.

Pivot the Data

pivoted <- pivot_longer(dataset, col=names(dataset[-1]), names_to="animal_name",values_to = "weight")
print.data.frame(pivoted)
              IPCC.Area        animal_name weight
1   Indian Subcontinent     Cattle...dairy  275.0
2   Indian Subcontinent Cattle...non.dairy  110.0
3   Indian Subcontinent          Buffaloes  295.0
4   Indian Subcontinent     Swine...market   28.0
5   Indian Subcontinent   Swine...breeding   28.0
6   Indian Subcontinent Chicken...Broilers    0.9
7   Indian Subcontinent   Chicken...Layers    1.8
8   Indian Subcontinent              Ducks    2.7
9   Indian Subcontinent            Turkeys    6.8
10  Indian Subcontinent              Sheep   28.0
11  Indian Subcontinent              Goats   30.0
12  Indian Subcontinent             Horses  238.0
13  Indian Subcontinent              Asses  130.0
14  Indian Subcontinent              Mules  130.0
15  Indian Subcontinent             Camels  217.0
16  Indian Subcontinent             Llamas  217.0
17       Eastern Europe     Cattle...dairy  550.0
18       Eastern Europe Cattle...non.dairy  391.0
19       Eastern Europe          Buffaloes  380.0
20       Eastern Europe     Swine...market   50.0
21       Eastern Europe   Swine...breeding  180.0
22       Eastern Europe Chicken...Broilers    0.9
23       Eastern Europe   Chicken...Layers    1.8
24       Eastern Europe              Ducks    2.7
25       Eastern Europe            Turkeys    6.8
26       Eastern Europe              Sheep   48.5
27       Eastern Europe              Goats   38.5
28       Eastern Europe             Horses  377.0
29       Eastern Europe              Asses  130.0
30       Eastern Europe              Mules  130.0
31       Eastern Europe             Camels  217.0
32       Eastern Europe             Llamas  217.0
33               Africa     Cattle...dairy  275.0
34               Africa Cattle...non.dairy  173.0
35               Africa          Buffaloes  380.0
36               Africa     Swine...market   28.0
37               Africa   Swine...breeding   28.0
38               Africa Chicken...Broilers    0.9
39               Africa   Chicken...Layers    1.8
40               Africa              Ducks    2.7
41               Africa            Turkeys    6.8
42               Africa              Sheep   28.0
43               Africa              Goats   30.0
44               Africa             Horses  238.0
45               Africa              Asses  130.0
46               Africa              Mules  130.0
47               Africa             Camels  217.0
48               Africa             Llamas  217.0
49              Oceania     Cattle...dairy  500.0
50              Oceania Cattle...non.dairy  330.0
51              Oceania          Buffaloes  380.0
52              Oceania     Swine...market   45.0
53              Oceania   Swine...breeding  180.0
54              Oceania Chicken...Broilers    0.9
55              Oceania   Chicken...Layers    1.8
56              Oceania              Ducks    2.7
57              Oceania            Turkeys    6.8
58              Oceania              Sheep   48.5
59              Oceania              Goats   38.5
60              Oceania             Horses  377.0
61              Oceania              Asses  130.0
62              Oceania              Mules  130.0
63              Oceania             Camels  217.0
64              Oceania             Llamas  217.0
65       Western Europe     Cattle...dairy  600.0
66       Western Europe Cattle...non.dairy  420.0
67       Western Europe          Buffaloes  380.0
68       Western Europe     Swine...market   50.0
69       Western Europe   Swine...breeding  198.0
70       Western Europe Chicken...Broilers    0.9
71       Western Europe   Chicken...Layers    1.8
72       Western Europe              Ducks    2.7
73       Western Europe            Turkeys    6.8
74       Western Europe              Sheep   48.5
75       Western Europe              Goats   38.5
76       Western Europe             Horses  377.0
77       Western Europe              Asses  130.0
78       Western Europe              Mules  130.0
79       Western Europe             Camels  217.0
80       Western Europe             Llamas  217.0
81        Latin America     Cattle...dairy  400.0
82        Latin America Cattle...non.dairy  305.0
83        Latin America          Buffaloes  380.0
84        Latin America     Swine...market   28.0
85        Latin America   Swine...breeding   28.0
86        Latin America Chicken...Broilers    0.9
87        Latin America   Chicken...Layers    1.8
88        Latin America              Ducks    2.7
89        Latin America            Turkeys    6.8
90        Latin America              Sheep   28.0
91        Latin America              Goats   30.0
92        Latin America             Horses  238.0
93        Latin America              Asses  130.0
94        Latin America              Mules  130.0
95        Latin America             Camels  217.0
96        Latin America             Llamas  217.0
97                 Asia     Cattle...dairy  350.0
98                 Asia Cattle...non.dairy  391.0
99                 Asia          Buffaloes  380.0
100                Asia     Swine...market   50.0
101                Asia   Swine...breeding  180.0
102                Asia Chicken...Broilers    0.9
103                Asia   Chicken...Layers    1.8
104                Asia              Ducks    2.7
105                Asia            Turkeys    6.8
106                Asia              Sheep   48.5
107                Asia              Goats   38.5
108                Asia             Horses  377.0
109                Asia              Asses  130.0
110                Asia              Mules  130.0
111                Asia             Camels  217.0
112                Asia             Llamas  217.0
113         Middle east     Cattle...dairy  275.0
114         Middle east Cattle...non.dairy  173.0
115         Middle east          Buffaloes  380.0
116         Middle east     Swine...market   28.0
117         Middle east   Swine...breeding   28.0
118         Middle east Chicken...Broilers    0.9
119         Middle east   Chicken...Layers    1.8
120         Middle east              Ducks    2.7
121         Middle east            Turkeys    6.8
122         Middle east              Sheep   28.0
123         Middle east              Goats   30.0
124         Middle east             Horses  238.0
125         Middle east              Asses  130.0
126         Middle east              Mules  130.0
127         Middle east             Camels  217.0
128         Middle east             Llamas  217.0
129    Northern America     Cattle...dairy  604.0
130    Northern America Cattle...non.dairy  389.0
131    Northern America          Buffaloes  380.0
132    Northern America     Swine...market   46.0
133    Northern America   Swine...breeding  198.0
134    Northern America Chicken...Broilers    0.9
135    Northern America   Chicken...Layers    1.8
136    Northern America              Ducks    2.7
137    Northern America            Turkeys    6.8
138    Northern America              Sheep   48.5
139    Northern America              Goats   38.5
140    Northern America             Horses  377.0
141    Northern America              Asses  130.0
142    Northern America              Mules  130.0
143    Northern America             Camels  217.0
144    Northern America             Llamas  217.0

After performing the pivot, we see that as expected there are three columns, and now each row corresponds to a single observance of the weight of an animal-location pair. However, I think the data would be easier to read if instead we sorted by animal name and made that feature the first column, since this would group the same animal names together sequentially. So, I performed the following operations:

pivoted <- pivoted[, c(2, 1, 3)]
pivoted <- arrange(pivoted, animal_name)
print.data.frame(pivoted)
           animal_name           IPCC.Area weight
1                Asses Indian Subcontinent  130.0
2                Asses      Eastern Europe  130.0
3                Asses              Africa  130.0
4                Asses             Oceania  130.0
5                Asses      Western Europe  130.0
6                Asses       Latin America  130.0
7                Asses                Asia  130.0
8                Asses         Middle east  130.0
9                Asses    Northern America  130.0
10           Buffaloes Indian Subcontinent  295.0
11           Buffaloes      Eastern Europe  380.0
12           Buffaloes              Africa  380.0
13           Buffaloes             Oceania  380.0
14           Buffaloes      Western Europe  380.0
15           Buffaloes       Latin America  380.0
16           Buffaloes                Asia  380.0
17           Buffaloes         Middle east  380.0
18           Buffaloes    Northern America  380.0
19              Camels Indian Subcontinent  217.0
20              Camels      Eastern Europe  217.0
21              Camels              Africa  217.0
22              Camels             Oceania  217.0
23              Camels      Western Europe  217.0
24              Camels       Latin America  217.0
25              Camels                Asia  217.0
26              Camels         Middle east  217.0
27              Camels    Northern America  217.0
28      Cattle...dairy Indian Subcontinent  275.0
29      Cattle...dairy      Eastern Europe  550.0
30      Cattle...dairy              Africa  275.0
31      Cattle...dairy             Oceania  500.0
32      Cattle...dairy      Western Europe  600.0
33      Cattle...dairy       Latin America  400.0
34      Cattle...dairy                Asia  350.0
35      Cattle...dairy         Middle east  275.0
36      Cattle...dairy    Northern America  604.0
37  Cattle...non.dairy Indian Subcontinent  110.0
38  Cattle...non.dairy      Eastern Europe  391.0
39  Cattle...non.dairy              Africa  173.0
40  Cattle...non.dairy             Oceania  330.0
41  Cattle...non.dairy      Western Europe  420.0
42  Cattle...non.dairy       Latin America  305.0
43  Cattle...non.dairy                Asia  391.0
44  Cattle...non.dairy         Middle east  173.0
45  Cattle...non.dairy    Northern America  389.0
46  Chicken...Broilers Indian Subcontinent    0.9
47  Chicken...Broilers      Eastern Europe    0.9
48  Chicken...Broilers              Africa    0.9
49  Chicken...Broilers             Oceania    0.9
50  Chicken...Broilers      Western Europe    0.9
51  Chicken...Broilers       Latin America    0.9
52  Chicken...Broilers                Asia    0.9
53  Chicken...Broilers         Middle east    0.9
54  Chicken...Broilers    Northern America    0.9
55    Chicken...Layers Indian Subcontinent    1.8
56    Chicken...Layers      Eastern Europe    1.8
57    Chicken...Layers              Africa    1.8
58    Chicken...Layers             Oceania    1.8
59    Chicken...Layers      Western Europe    1.8
60    Chicken...Layers       Latin America    1.8
61    Chicken...Layers                Asia    1.8
62    Chicken...Layers         Middle east    1.8
63    Chicken...Layers    Northern America    1.8
64               Ducks Indian Subcontinent    2.7
65               Ducks      Eastern Europe    2.7
66               Ducks              Africa    2.7
67               Ducks             Oceania    2.7
68               Ducks      Western Europe    2.7
69               Ducks       Latin America    2.7
70               Ducks                Asia    2.7
71               Ducks         Middle east    2.7
72               Ducks    Northern America    2.7
73               Goats Indian Subcontinent   30.0
74               Goats      Eastern Europe   38.5
75               Goats              Africa   30.0
76               Goats             Oceania   38.5
77               Goats      Western Europe   38.5
78               Goats       Latin America   30.0
79               Goats                Asia   38.5
80               Goats         Middle east   30.0
81               Goats    Northern America   38.5
82              Horses Indian Subcontinent  238.0
83              Horses      Eastern Europe  377.0
84              Horses              Africa  238.0
85              Horses             Oceania  377.0
86              Horses      Western Europe  377.0
87              Horses       Latin America  238.0
88              Horses                Asia  377.0
89              Horses         Middle east  238.0
90              Horses    Northern America  377.0
91              Llamas Indian Subcontinent  217.0
92              Llamas      Eastern Europe  217.0
93              Llamas              Africa  217.0
94              Llamas             Oceania  217.0
95              Llamas      Western Europe  217.0
96              Llamas       Latin America  217.0
97              Llamas                Asia  217.0
98              Llamas         Middle east  217.0
99              Llamas    Northern America  217.0
100              Mules Indian Subcontinent  130.0
101              Mules      Eastern Europe  130.0
102              Mules              Africa  130.0
103              Mules             Oceania  130.0
104              Mules      Western Europe  130.0
105              Mules       Latin America  130.0
106              Mules                Asia  130.0
107              Mules         Middle east  130.0
108              Mules    Northern America  130.0
109              Sheep Indian Subcontinent   28.0
110              Sheep      Eastern Europe   48.5
111              Sheep              Africa   28.0
112              Sheep             Oceania   48.5
113              Sheep      Western Europe   48.5
114              Sheep       Latin America   28.0
115              Sheep                Asia   48.5
116              Sheep         Middle east   28.0
117              Sheep    Northern America   48.5
118   Swine...breeding Indian Subcontinent   28.0
119   Swine...breeding      Eastern Europe  180.0
120   Swine...breeding              Africa   28.0
121   Swine...breeding             Oceania  180.0
122   Swine...breeding      Western Europe  198.0
123   Swine...breeding       Latin America   28.0
124   Swine...breeding                Asia  180.0
125   Swine...breeding         Middle east   28.0
126   Swine...breeding    Northern America  198.0
127     Swine...market Indian Subcontinent   28.0
128     Swine...market      Eastern Europe   50.0
129     Swine...market              Africa   28.0
130     Swine...market             Oceania   45.0
131     Swine...market      Western Europe   50.0
132     Swine...market       Latin America   28.0
133     Swine...market                Asia   50.0
134     Swine...market         Middle east   28.0
135     Swine...market    Northern America   46.0
136            Turkeys Indian Subcontinent    6.8
137            Turkeys      Eastern Europe    6.8
138            Turkeys              Africa    6.8
139            Turkeys             Oceania    6.8
140            Turkeys      Western Europe    6.8
141            Turkeys       Latin America    6.8
142            Turkeys                Asia    6.8
143            Turkeys         Middle east    6.8
144            Turkeys    Northern America    6.8