Import the needed files as .csv
files
# require tidyverse
require(tidyverse)
## Loading required package: tidyverse
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.7 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
Part1 <- read_csv("https://github.com/mbtoomey/Biol_7263/blob/main/Data/assignment6part1.csv?raw=true")
## Rows: 2 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): ID
## dbl (20): Sample1_Male_Control, Sample2_Male_Control, Sample3_Male_Control, ...
##
## ℹ 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.
Part2 <- read_csv("https://github.com/mbtoomey/Biol_7263/blob/main/Data/assignment6part2.csv?raw=true")
## Rows: 1 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): ID
## dbl (16): Sample16.Treatment, Sample12.Control, Sample3.Control, Sample6.Tre...
##
## ℹ 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.
#NOTE: must include "?raw=true" at the end of the link to download the data successfully
1. Pivot and merge these two data sets into a single tidy tibble.
Export this tibble as a .csv file saved to a folder called “Results”
folder within your R project.
# pivot the tibbles so variable names are columns and each sample has its own row
Part1_tib <- Part1 %>% pivot_longer(cols = !ID, # break up the sample name into 3 columns
names_to = c("Sample", "Sex", "Experiment"), # set the new column names
names_sep = "_") %>% # new column variable deliminator
pivot_wider(names_from = ID, values_from = value) # create two columns for body length and age
Part2_tib <- Part2 %>% pivot_longer(col = !ID, # break up the sample name into a column
names_to = c("Sample"), # set the new column name
names_pattern = "(Sample1?.)..*") %>% # new column (variable)
pivot_wider(names_from = ID, values_from = value) # create a column for mass
# merge the tibbles using Sample fields
write_csv(Part1_tib %>%
inner_join(Part2_tib, by = "Sample"), # will only join by a common field
"Results/Assign5_Merge.csv") # export the `.csv` to the Results folder
2. With this tidy tibble, generate a new tibble of the mean +/-
standard deviation of the residual mass (mass/body length) by treatment
and sex. Export this tibble as a .csv file saved to a folder called
“Results” folder within your R project.
# load the new tibble and create a residual mass variable
Assign5 <- read_csv("Results/Assign5_Merge.csv")
## Rows: 16 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Sample, Sex, Experiment
## dbl (3): body_length, age, mass
##
## ℹ 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.
Assign5 <- mutate(Assign5, resid_mass = mass/body_length)
# group the samples by Experiment type
Assign5 <- group_by(Assign5, Experiment)
# calculate the stddev of the resid_mass by Experiment type
Exp_SD <- summarize(Assign5, mean_rmass=mean(resid_mass, na.rm=TRUE),
SD_rmass=sd(resid_mass, na.rm=TRUE))
# ungroup and regroup by sex
ungroup(Assign5)
## # A tibble: 16 × 7
## Sample Sex Experiment body_length age mass resid_mass
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Sample3 Male Control 4.54 11 8.46 1.86
## 2 Sample4 Male Control 1.09 8 3.87 3.56
## 3 Sample5 Male Control 3.55 10 3.12 0.878
## 4 Sample6 Male Treatment 8.19 4 9.38 1.15
## 5 Sample7 Male Treatment 8.45 8 7.14 0.845
## 6 Sample8 Male Treatment 0.893 11 7.41 8.29
## 7 Sample10 Male Treatment 8.46 3 12.3 1.45
## 8 Sample11 Female Control 2.90 10 19.8 6.82
## 9 Sample12 Female Control 5.37 5 3.99 0.743
## 10 Sample13 Female Control 2.88 3 8.65 3.00
## 11 Sample14 Female Control 3.82 3 9.06 2.37
## 12 Sample15 Female Control 4.15 1 5.01 1.21
## 13 Sample16 Female Treatment 1.73 7 10.1 5.82
## 14 Sample17 Female Treatment 2.03 0 8.80 4.34
## 15 Sample18 Female Treatment 3.85 4 1.11 0.288
## 16 Sample19 Female Treatment 10.7 6 16.1 1.50
Assign5 <- group_by(Assign5, Sex)
# calculate the stddev of the residual_mass by sex
Sex_SD <- summarize(Assign5, mean_rmass=mean(resid_mass, na.rm=TRUE),
SD_rmass=sd(resid_mass, na.rm=TRUE))
# combine the tibbles
write_csv(Exp_SD %>%
full_join(Sex_SD), # will join by the common fields
"Results/Assign5_SD_ExpXSex.csv") # export to the Results folder
## Joining, by = c("mean_rmass", "SD_rmass")