library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ 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
  1. Read in the ChickWeight dataset. This data is from an experiment on the effect of diet on early growth of chicks. Call it chicks.
chicks <- datasets::ChickWeight
  1. Reshape to wide format using pivot_wider. Use the arguments names_from = Time and values_from = weight. Include the following in the pivot_wider function: names_prefix = "Time_" to update all the new column names. Call the new data chicks_wide.
chicks_wide <- chicks %>% pivot_wider(names_from = Time, values_from = weight, names_prefix = "Time_")
chicks_wide
## # A tibble: 50 × 14
##    Chick Diet  Time_0 Time_2 Time_4 Time_6 Time_8 Time_10 Time_12 Time_14
##    <ord> <fct>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>
##  1 1     1         42     51     59     64     76      93     106     125
##  2 2     1         40     49     58     72     84     103     122     138
##  3 3     1         43     39     55     67     84      99     115     138
##  4 4     1         42     49     56     67     74      87     102     108
##  5 5     1         41     42     48     60     79     106     141     164
##  6 6     1         41     49     59     74     97     124     141     148
##  7 7     1         41     49     57     71     89     112     146     174
##  8 8     1         42     50     61     71     84      93     110     116
##  9 9     1         42     51     59     68     85      96      90      92
## 10 10    1         41     44     52     63     74      81      89      96
## # ℹ 40 more rows
## # ℹ 4 more variables: Time_16 <dbl>, Time_18 <dbl>, Time_20 <dbl>,
## #   Time_21 <dbl>
  1. Filter the data so that Time_0 is <= 40 and Time_21 >= 200. Reassign to chicks_wide.
chicks_wide <- chicks_wide %>% filter(Time_0 <= 40 & Time_21 >= 200)
chicks_wide
## # A tibble: 9 × 14
##   Chick Diet  Time_0 Time_2 Time_4 Time_6 Time_8 Time_10 Time_12 Time_14 Time_16
##   <ord> <fct>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
## 1 2     1         40     49     58     72     84     103     122     138     162
## 2 21    2         40     50     62     86    125     163     217     240     275
## 3 25    2         40     49     62     78    102     124     146     164     197
## 4 28    2         39     46     58     73     92     114     145     156     184
## 5 29    2         39     48     59     74     87     106     134     150     187
## 6 36    3         39     48     61     76     98     116     145     166     198
## 7 46    4         40     52     62     82    101     120     144     156     173
## 8 48    4         39     50     62     80    104     125     154     170     222
## 9 49    4         40     53     64     85    108     128     152     166     184
## # ℹ 3 more variables: Time_18 <dbl>, Time_20 <dbl>, Time_21 <dbl>
  1. Select columns Chick, Diet, Time_0, and Time_21. Reassign to chicks_wide.
chicks_wide <- chicks_wide %>% select(Chick, Diet, Time_0, Time_21)
chicks_wide
## # A tibble: 9 × 4
##   Chick Diet  Time_0 Time_21
##   <ord> <fct>  <dbl>   <dbl>
## 1 2     1         40     215
## 2 21    2         40     331
## 3 25    2         40     265
## 4 28    2         39     233
## 5 29    2         39     309
## 6 36    3         39     220
## 7 46    4         40     238
## 8 48    4         39     322
## 9 49    4         40     237
  1. Reshape to long format using pivot_longer. Pivot the numeric columns. Use the arguments names_to = and values_to =. Call the data chicks_long.
chicks_wide %>% pivot_longer(c(Time_0, Time_21), names_to = "Time", values_to = "weight")
## # A tibble: 18 × 4
##    Chick Diet  Time    weight
##    <ord> <fct> <chr>    <dbl>
##  1 2     1     Time_0      40
##  2 2     1     Time_21    215
##  3 21    2     Time_0      40
##  4 21    2     Time_21    331
##  5 25    2     Time_0      40
##  6 25    2     Time_21    265
##  7 28    2     Time_0      39
##  8 28    2     Time_21    233
##  9 29    2     Time_0      39
## 10 29    2     Time_21    309
## 11 36    3     Time_0      39
## 12 36    3     Time_21    220
## 13 46    4     Time_0      40
## 14 46    4     Time_21    238
## 15 48    4     Time_0      39
## 16 48    4     Time_21    322
## 17 49    4     Time_0      40
## 18 49    4     Time_21    237