R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.3
## Warning: package 'ggplot2' was built under R version 4.5.3
## Warning: package 'tidyr' was built under R version 4.5.3
## Warning: package 'purrr' was built under R version 4.5.3
## Warning: package 'dplyr' was built under R version 4.5.3
## Warning: package 'stringr' was built under R version 4.5.3
## Warning: package 'forcats' was built under R version 4.5.3
## Warning: package 'lubridate' was built under R version 4.5.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.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
url <- "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-01/key_crop_yields.csv"
df_crop <- read_csv(url)
## Rows: 13075 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): Entity, Code
## dbl (12): Year, Wheat (tonnes per hectare), Rice (tonnes per hectare), Maize...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
glimpse(df_crop)
## Rows: 13,075
## Columns: 14
## $ Entity                             <chr> "Afghanistan", "Afghanistan", "Afgh…
## $ Code                               <chr> "AFG", "AFG", "AFG", "AFG", "AFG", …
## $ Year                               <dbl> 1961, 1962, 1963, 1964, 1965, 1966,…
## $ `Wheat (tonnes per hectare)`       <dbl> 1.0220, 0.9735, 0.8317, 0.9510, 0.9…
## $ `Rice (tonnes per hectare)`        <dbl> 1.5190, 1.5190, 1.5190, 1.7273, 1.7…
## $ `Maize (tonnes per hectare)`       <dbl> 1.4000, 1.4000, 1.4260, 1.4257, 1.4…
## $ `Soybeans (tonnes per hectare)`    <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ `Potatoes (tonnes per hectare)`    <dbl> 8.6667, 7.6667, 8.1333, 8.6000, 8.8…
## $ `Beans (tonnes per hectare)`       <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ `Peas (tonnes per hectare)`        <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ `Cassava (tonnes per hectare)`     <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ `Barley (tonnes per hectare)`      <dbl> 1.0800, 1.0800, 1.0800, 1.0857, 1.0…
## $ `Cocoa beans (tonnes per hectare)` <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ `Bananas (tonnes per hectare)`     <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA,…
df_crop %>%
  select(
    Entity,
    Year,
    `Potatoes (tonnes per hectare)`,
    `Cassava (tonnes per hectare)`
  )
## # A tibble: 13,075 × 4
##    Entity       Year `Potatoes (tonnes per hectare)` Cassava (tonnes per hecta…¹
##    <chr>       <dbl>                           <dbl>                       <dbl>
##  1 Afghanistan  1961                            8.67                          NA
##  2 Afghanistan  1962                            7.67                          NA
##  3 Afghanistan  1963                            8.13                          NA
##  4 Afghanistan  1964                            8.6                           NA
##  5 Afghanistan  1965                            8.8                           NA
##  6 Afghanistan  1966                            9.07                          NA
##  7 Afghanistan  1967                            9.8                           NA
##  8 Afghanistan  1968                           10                             NA
##  9 Afghanistan  1969                           10.2                           NA
## 10 Afghanistan  1970                            9.54                          NA
## # ℹ 13,065 more rows
## # ℹ abbreviated name: ¹​`Cassava (tonnes per hectare)`
df_crop %>%
  select(
    -`Soybeans (tonnes per hectare)`,
    -`Beans (tonnes per hectare)`,
    -`Peas (tonnes per hectare)`
  )
## # A tibble: 13,075 × 11
##    Entity      Code   Year `Wheat (tonnes per hectare)` Rice (tonnes per hecta…¹
##    <chr>       <chr> <dbl>                        <dbl>                    <dbl>
##  1 Afghanistan AFG    1961                        1.02                      1.52
##  2 Afghanistan AFG    1962                        0.974                     1.52
##  3 Afghanistan AFG    1963                        0.832                     1.52
##  4 Afghanistan AFG    1964                        0.951                     1.73
##  5 Afghanistan AFG    1965                        0.972                     1.73
##  6 Afghanistan AFG    1966                        0.867                     1.52
##  7 Afghanistan AFG    1967                        1.12                      1.92
##  8 Afghanistan AFG    1968                        1.16                      1.95
##  9 Afghanistan AFG    1969                        1.19                      1.98
## 10 Afghanistan AFG    1970                        0.956                     1.81
## # ℹ 13,065 more rows
## # ℹ abbreviated name: ¹​`Rice (tonnes per hectare)`
## # ℹ 6 more variables: `Maize (tonnes per hectare)` <dbl>,
## #   `Potatoes (tonnes per hectare)` <dbl>,
## #   `Cassava (tonnes per hectare)` <dbl>, `Barley (tonnes per hectare)` <dbl>,
## #   `Cocoa beans (tonnes per hectare)` <dbl>,
## #   `Bananas (tonnes per hectare)` <dbl>
df_crop %>%
  filter(
    Entity == "Indonesia",
    `Rice (tonnes per hectare)` < 2
  ) %>%
  select(
    Entity,
    Year,
    `Rice (tonnes per hectare)`
  )
## # A tibble: 7 × 3
##   Entity     Year `Rice (tonnes per hectare)`
##   <chr>     <dbl>                       <dbl>
## 1 Indonesia  1961                        1.76
## 2 Indonesia  1962                        1.79
## 3 Indonesia  1963                        1.72
## 4 Indonesia  1964                        1.76
## 5 Indonesia  1965                        1.77
## 6 Indonesia  1966                        1.77
## 7 Indonesia  1967                        1.76
df_crop %>%
  filter(
    Year >= 2000,
    `Wheat (tonnes per hectare)` > 5
  ) %>%
  select(
    Entity,
    Year,
    `Wheat (tonnes per hectare)`
  )
## # A tibble: 424 × 3
##    Entity   Year `Wheat (tonnes per hectare)`
##    <chr>   <dbl>                        <dbl>
##  1 Austria  2001                         5.24
##  2 Austria  2004                         5.92
##  3 Austria  2005                         5.03
##  4 Austria  2008                         5.69
##  5 Austria  2010                         5.01
##  6 Austria  2011                         5.85
##  7 Austria  2013                         5.37
##  8 Austria  2014                         5.92
##  9 Austria  2015                         5.70
## 10 Austria  2016                         6.25
## # ℹ 414 more rows
df_crop %>%
  filter(
    Entity %in% c("Indonesia", "Malaysia"),
    Year == 2015
  )
## # A tibble: 2 × 14
##   Entity    Code   Year `Wheat (tonnes per hectare)` `Rice (tonnes per hectare)`
##   <chr>     <chr> <dbl>                        <dbl>                       <dbl>
## 1 Indonesia IDN    2015                           NA                        5.34
## 2 Malaysia  MYS    2015                           NA                        4.02
## # ℹ 9 more variables: `Maize (tonnes per hectare)` <dbl>,
## #   `Soybeans (tonnes per hectare)` <dbl>,
## #   `Potatoes (tonnes per hectare)` <dbl>, `Beans (tonnes per hectare)` <dbl>,
## #   `Peas (tonnes per hectare)` <dbl>, `Cassava (tonnes per hectare)` <dbl>,
## #   `Barley (tonnes per hectare)` <dbl>,
## #   `Cocoa beans (tonnes per hectare)` <dbl>,
## #   `Bananas (tonnes per hectare)` <dbl>
df_crop %>%
  filter(Year == 2020) %>%
  arrange(`Maize (tonnes per hectare)`) %>%
  slice(1) %>%
  select(
    Entity,
    Year,
    `Maize (tonnes per hectare)`
  )
## # A tibble: 0 × 3
## # ℹ 3 variables: Entity <chr>, Year <dbl>, Maize (tonnes per hectare) <dbl>
df_crop %>%
  filter(Entity == "Indonesia") %>%
  arrange(desc(`Potatoes (tonnes per hectare)`))
## # A tibble: 58 × 14
##    Entity    Code   Year `Wheat (tonnes per hectare)` Rice (tonnes per hectare…¹
##    <chr>     <chr> <dbl>                        <dbl>                      <dbl>
##  1 Indonesia IDN    2018                           NA                       5.19
##  2 Indonesia IDN    2016                           NA                       5.24
##  3 Indonesia IDN    2015                           NA                       5.34
##  4 Indonesia IDN    2014                           NA                       5.13
##  5 Indonesia IDN    2006                           NA                       4.62
##  6 Indonesia IDN    2008                           NA                       4.89
##  7 Indonesia IDN    1995                           NA                       4.35
##  8 Indonesia IDN    2012                           NA                       5.14
##  9 Indonesia IDN    2009                           NA                       5.00
## 10 Indonesia IDN    2005                           NA                       4.57
## # ℹ 48 more rows
## # ℹ abbreviated name: ¹​`Rice (tonnes per hectare)`
## # ℹ 9 more variables: `Maize (tonnes per hectare)` <dbl>,
## #   `Soybeans (tonnes per hectare)` <dbl>,
## #   `Potatoes (tonnes per hectare)` <dbl>, `Beans (tonnes per hectare)` <dbl>,
## #   `Peas (tonnes per hectare)` <dbl>, `Cassava (tonnes per hectare)` <dbl>,
## #   `Barley (tonnes per hectare)` <dbl>, …
df_crop %>%
  mutate(
    Rice_Status = if_else(
      `Rice (tonnes per hectare)` > 4,
      "Tinggi",
      "Rendah"
    )
  ) %>%
  select(
    Entity,
    Year,
    `Rice (tonnes per hectare)`,
    Rice_Status
  )
## # A tibble: 13,075 × 4
##    Entity       Year `Rice (tonnes per hectare)` Rice_Status
##    <chr>       <dbl>                       <dbl> <chr>      
##  1 Afghanistan  1961                        1.52 Rendah     
##  2 Afghanistan  1962                        1.52 Rendah     
##  3 Afghanistan  1963                        1.52 Rendah     
##  4 Afghanistan  1964                        1.73 Rendah     
##  5 Afghanistan  1965                        1.73 Rendah     
##  6 Afghanistan  1966                        1.52 Rendah     
##  7 Afghanistan  1967                        1.92 Rendah     
##  8 Afghanistan  1968                        1.95 Rendah     
##  9 Afghanistan  1969                        1.98 Rendah     
## 10 Afghanistan  1970                        1.81 Rendah     
## # ℹ 13,065 more rows
df_crop %>%
  filter(Entity == "Indonesia") %>%
  summarise(
    Rata_rata_Bananas =
      mean(`Bananas (tonnes per hectare)`, na.rm = TRUE)
  )
## # A tibble: 1 × 1
##   Rata_rata_Bananas
##               <dbl>
## 1              30.5
df_crop %>%
  filter(Year >= 2010) %>%
  group_by(Entity) %>%
  summarise(
    SD_Maize =
      sd(`Maize (tonnes per hectare)`, na.rm = TRUE)
  ) %>%
  arrange(desc(SD_Maize))
## # A tibble: 242 × 2
##    Entity                           SD_Maize
##    <chr>                               <dbl>
##  1 Kuwait                               9.24
##  2 United Arab Emirates                 9.19
##  3 Jordan                               7.03
##  4 Israel                               4.80
##  5 Saint Vincent and the Grenadines     2.89
##  6 Qatar                                2.74
##  7 French Guiana                        2.50
##  8 New Caledonia                        2.29
##  9 Slovakia                             1.68
## 10 Oman                                 1.61
## # ℹ 232 more rows