Challenge3_MarcelaRobinson(animal_weights)

Author

Marcela Robinson

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0     ✔ purrr   1.0.1
✔ tibble  3.1.8     ✔ dplyr   1.1.0
✔ tidyr   1.3.0     ✔ stringr 1.5.0
✔ readr   2.1.3     ✔ forcats 1.0.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(readr)

##Read in “animal_weight.csv”

animal_weights<-read.csv("_data/animal_weight.csv")
animal_weights
            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

The csv file “animal_weights” contains the weight of 16 different animals (Cattle - dairy, Cattle - non-dairy, Buffaloes, Swine - market, Swine - breeding, Chicken - Broilers, Chicken - Layers, Ducks, Turkeys, Sheep, Goats, Horses, Asses, Mules, Camels, and Llamas) in 9 different areas of the world (Indian Subcontinent, Eastern Europe, Africa, Oceania,Western Europe,Latin America, Asia, Middle East and Northern America). This data was provided by the Intergovernmental Panel on Climate Change, a United Nations body assessing the science related to climate change.

This dataset can provide relevant information to determine underfeeding/overfeeding in certain areas of the world and check the measurements with the standards of the industry. It can also be useful in determining market values for the animals.

The dataset is wide and difficult to read. Therefore, I want to use the function pivot_longer() to make the data longer. However, I first want to check on what is the expected row/cases for the data.

##Determine the expected row/cases

##Existing rows/cases
nrow(animal_weights)
[1] 9
##Existing column/cases
ncol(animal_weights)
[1] 17
##Expected row/cases
nrow(animal_weights)*(ncol(animal_weights)-1)
[1] 144

According to the test, my new dataframe should have 144 rows.

##Pivot_Longer the data #Pivot every column except the first

animal_weights_PL<- pivot_longer(animal_weights, cols = -1, names_to = "Animals", values_to = "Weights")
animal_weights_PL
# A tibble: 144 × 3
   IPCC.Area           Animals            Weights
   <chr>               <chr>                <dbl>
 1 Indian Subcontinent Cattle...dairy       275  
 2 Indian Subcontinent Cattle...non.dairy   110  
 3 Indian Subcontinent Buffaloes            295  
 4 Indian Subcontinent Swine...market        28  
 5 Indian Subcontinent Swine...breeding      28  
 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  
# … with 134 more rows

I first wanted to pivot the data longer to decrease the number of columns and create a couple of new columns called “Animals” and “Weights”. The first column “Animals”, contains the names of the animals. The second column, “Weights” contains the weight for each of the animals. Once I pivot my data, they were 144 observations and 3 variables.