Go to basic R
Go to import data
Go to making figures
Exercise 1. Import and Select subject
, timepoint
and sj.max
from the cycling study data set. Print your results. What does printing do?
Solution Ex. 1
# load packages
library(tidyverse)
library(readxl)
cyclingStudy <- read_excel("./data/cyclingStudy.xlsx", na = "NA") # remember to use the na argument
cyclingStudy %>%
select(subject, timepoint, sj.max) %>%
print()
Alternatively, everything can be written in one pipe:
cyclingStudy <- read_excel("./data/cyclingStudy.xlsx", na = "NA") %>% # remember to use the na argument
select(subject, timepoint, sj.max) %>%
print()
Exercise 2. Filter the cycling data set based on time-point keep only observations from time-point pre.
Solution Ex. 2
cyclingStudy <- read_excel("./data/cyclingStudy.xlsx", na = "NA") %>% # remember to use the na argument
filter(timepoint == "pre") %>%
print()
Exercise 3. Select only columns subject
and sj.max
. Filter the cycling data set based on time-point keep only observations from time-point pre and meso1. . In what order must you do the operations in order to get it to work?
Solution Ex. 3
cyclingStudy <- read_excel("./data/cyclingStudy.xlsx", na = "NA") %>% # remember to use the na argument
filter(timepoint %in% c("pre", "meso1")) %>%
select(subject, sj.max) %>%
print()
Exercise 4. weight.T1
is the weight in kg and VO2.max
is the \(\dot VO_{2max}\) in \(ml \times min^{-1}\) from each test. Select the needed variables, filter needed observations, create a new variable \(\dot VO_{2max} / kg\) and calculate the percentage change from time-point “pre” to “meso3”. This operation should include mutate()
, filter()
, pivot_wider
.
Solution Ex. 4
read_excel("./data/cyclingStudy.xlsx", na = "NA") %>% # remember to use the na argument
filter(timepoint %in% c("pre", "meso3")) %>%
select(subject, timepoint, VO2.max, weight.T1) %>%
mutate(relative.vo2max = VO2.max / weight.T1) %>%
select(subject, timepoint, relative.vo2max) %>%
pivot_wider(names_from = timepoint, values_from = relative.vo2max) %>%
mutate(change = ((meso3 / pre)-1)*100) %>%
print()
Exercise 5. Do the same as above but keep the group
variable. Calculate the mean of each group in percentage change from pre- to meso3. In the data set there are three groups: “INCR” increased intensity through the training period, “DECR” decreased intensity and “MIX” performed a mix of intensities each week.
Solution Ex. 5
read_excel("./data/cyclingStudy.xlsx", na = "NA") %>% # remember to use the na argument
filter(timepoint %in% c("pre", "meso3")) %>%
select(subject, group, timepoint, VO2.max, weight.T1) %>%
mutate(relative.vo2max = VO2.max / weight.T1) %>%
select(subject, group, timepoint, relative.vo2max) %>%
pivot_wider(names_from = timepoint, values_from = relative.vo2max) %>%
mutate(change = ((meso3 / pre)-1)*100) %>%
group_by(group) %>%
summarise(m = mean(change, na.rm = TRUE)) %>%
print()
The n()
function can be used with summarise()
. n()
returns the number of observations including NAs. We can include n()
to count the number of observations. If we are only interested in values that are not NA we could use the sum()
function and count the number of values that are not NA. is.na()
says TRUE if it finds a NA. if we inverse it, we will get a counter of non NAs
read_excel("./data/cyclingStudy.xlsx", na = "NA") %>% # remember to use the na argument
filter(timepoint %in% c("pre", "meso3")) %>%
select(subject, group, timepoint, VO2.max, weight.T1) %>%
mutate(relative.vo2max = VO2.max / weight.T1) %>%
select(subject, group, timepoint, relative.vo2max) %>%
pivot_wider(names_from = timepoint, values_from = relative.vo2max) %>%
mutate(change = ((meso3 / pre)-1)*100) %>%
group_by(group) %>%
summarise(m = mean(change, na.rm = TRUE),
n = n(),
non.na = sum(!is.na(change))) %>%
print()
Exercise 6. \(\dot VO_2\) meassures were collected during submaximal exercise, variables VO2.125:VO2.375
are \(\dot VO_2\) measurements from each watt. Transform the data to get this to tidy format.
Solution Ex. 6
read_excel("./data/cyclingStudy.xlsx", na = "NA") %>% # remember to use the na argument
select(subject, timepoint, VO2.125:VO2.375) %>%
pivot_longer(names_to = "watt",
names_prefix = "VO2.",
names_ptypes = list(watt = numeric()),
values_to = "VO2",
cols = VO2.125:VO2.375) %>%
print()
Exercise 7. \(\dot VO_2\) meassures were collected during submaximal exercise, variables VO2.125:VO2.375
are \(\dot VO_2\) measurements from each watt. Transform the data to get this to tidy format. Calculate the mean \(\dot VO_2\) for each group, timepoint and watt.
Solution Ex. 7
read_excel("./data/cyclingStudy.xlsx", na = "NA") %>% # remember to use the na argument
select(subject, group, timepoint, VO2.125:VO2.375) %>%
pivot_longer(names_to = "watt",
names_prefix = "VO2.",
names_ptypes = list(watt = numeric()),
values_to = "VO2",
cols = VO2.125:VO2.375) %>%
group_by(group, timepoint, watt) %>%
summarise(vo2 = mean(VO2, na.rm = TRUE)) %>%
print()