A very brief..
Intro to R







Mladen Čučak

mladencucak@gmail.com

Topics

  • About R/RStudio
  • Basics of programming with R
  • Data analysis with tidyverse









These materials are based on the APS's “R for Plant Pathologists” “R for Plant Pathologists”
Some inspiration from J. Bryan's Stat545 and B. Bohemke's Intro to R
All highly recommended

Why R


  • Performance: stable, light and fast

  • Support network: documentation, community, developers

  • Reproducibility: anyone anywhere can reproduce results

  • Versatility: unified solution to almost any numerical problem and graphical capabilities

  • Ethics: accessible to anyone as it is free and open source

Be strong!

Transition from “point and click” is tough but rewarding

Reshaping data: wide

Important for data visualization

Our data subset is in long format

dt_small
# A tibble: 6 x 3
  cultivar zone     inc
  <chr>    <chr>  <dbl>
1 Improved Sheka   33.2
2 Improved Sidama  16.5
3 Local    Sheka   81.8
4 Local    Sidama  35.2
5 Mixture  Sheka   29.5
6 Mixture  Sidama  18.6

Change it to wide format with tidyr

  • names_from: column to columnS
  • values_from: column to values
(dt_small_wide <- 
dt_small %>%
  pivot_wider(names_from = "zone", 
              values_from = "inc"))
# A tibble: 3 x 3
  cultivar Sheka Sidama
  <chr>    <dbl>  <dbl>
1 Improved  33.2   16.5
2 Local     81.8   35.2
3 Mixture   29.5   18.6

Reshaping data: long




Can we do it the other way around?

dt_small_wide 
# A tibble: 3 x 3
  cultivar Sheka Sidama
  <chr>    <dbl>  <dbl>
1 Improved  33.2   16.5
2 Local     81.8   35.2
3 Mixture   29.5   18.6

Change it to long format with pivot_longer()

  • cols: columns to column
  • values_from: values to columns
dt_small_wide %>% 
  pivot_longer(cols = 
                 c("Sheka", "Sidama"), 
               names_to = "zone",
               values_to = "inc")
# A tibble: 6 x 3
  cultivar zone     inc
  <chr>    <chr>  <dbl>
1 Improved Sheka   33.2
2 Improved Sidama  16.5
3 Local    Sheka   81.8
4 Local    Sidama  35.2
5 Mixture  Sheka   29.5
6 Mixture  Sidama  18.6





Congratulations!!

So, the painful part is done, enjoy the rest!