Exercise of {tidyverse}

Introduction

This page contains several tests. The output of each answer is shown in order to help you understand the questions. The code itself is hidden and you can unfold them when needed. It is recommended that you go through the basic before taking the following exercises.

Exercise 1

Given the data:

tbl <- as_tibble(PlantGrowth)
tbl
# A tibble: 30 × 2
   weight group
    <dbl> <fct>
 1   4.17 ctrl 
 2   5.58 ctrl 
 3   5.18 ctrl 
 4   6.11 ctrl 
 5   4.5  ctrl 
 6   4.61 ctrl 
 7   5.17 ctrl 
 8   4.53 ctrl 
 9   5.33 ctrl 
10   5.14 ctrl 
# ℹ 20 more rows

Use the original tbl, create a new column “Kg” and assume that the weight columns is in tons that you convert to Kg. Afterwards, delete the weight column. Keep only values smaller than 5000 Kg. Sort the data so that the largest Kg are on top

Code
tbl %>%
    mutate(Kg = weight * 1000) %>%
    select(-weight) %>%
    filter(Kg < 5000) %>%
    arrange(desc(Kg))
# A tibble: 13 × 2
   group    Kg
   <fct> <dbl>
 1 trt2   4920
 2 trt1   4890
 3 trt1   4810
 4 trt1   4690
 5 ctrl   4610
 6 ctrl   4530
 7 ctrl   4500
 8 trt1   4410
 9 trt1   4320
10 ctrl   4170
11 trt1   4170
12 trt1   3830
13 trt1   3590

Exercise 2

Given the data dat:

path <- "https://raw.githubusercontent.com/SchmidtPaul/dsfair_quarto/master/data/Mead1993.csv"
dat <- read_csv(path, col_types = cols()) # use path from above
dat
# A tibble: 24 × 4
   variety yield   row   col
   <chr>   <dbl> <dbl> <dbl>
 1 v1       25.1     4     2
 2 v1       17.2     1     6
 3 v1       26.4     4     1
 4 v1       16.1     1     4
 5 v1       22.2     1     2
 6 v1       15.9     2     4
 7 v2       40.2     4     4
 8 v2       35.2     3     1
 9 v2       32.0     4     6
10 v2       36.5     2     1
# ℹ 14 more rows

Find out the average, minimum and maximum yields per variety using summarise(). Arrage as the highest value on the top.

Code
dat %>%
    group_by(variety) %>%
    summarise(
        avg_yield = mean(yield),
        min_yield = min(yield),
        max_yield = max(yield)
    ) %>%
    arrange(desc(avg_yield))
# A tibble: 4 × 4
  variety avg_yield min_yield max_yield
  <chr>       <dbl>     <dbl>     <dbl>
1 v2           37.4      32.0      43.3
2 v4           29.9      27.6      33.2
3 v1           20.5      15.9      26.4
4 v3           19.5      11.4      25.9