#must first install to be able to pull data # then must reference library install.packages(“tidyverse”) library(tidyverse)

#starwars part of tidyverse View(starwars)

#pick a varriable in the subgroup and report the average summarise(starwars, average_height = mean(height)) #use help to check for errors bc above line dosen’t run help(mean) ß #proper way to pull avg height (shoutout help fuction) # note that na.rm = TRUE is added to say that na values should not be included in finding avg # aka says (height, AND na values are removed) summarise(starwars, average_height = mean(height, na.rm = TRUE)) summarise(starwars, average_mass = mean(mass, na.rm = TRUE))

#use dplyr/pipe for a more on avg functions # (pipe operator is %>%) # PIPE IS A WAY TO CHAIN FUNCTIONS TOGETHER CONSICESLY AND SEQUENCELY, IT TAKES OUTPUT ON LEFT/ABOVE AND USES IT AS INPUT RIGHT/BELOW…….IT PASSES IT ON # so below is just another way to do above summarise function in a better way starwars %>% summarise(average_height = mean(height, na.rm = TRUE), average_mass = mean(mass, na.rm = TRUE))

#a set of compands under the umbrella of dplyr: group_by # groups data y set of standards, ex: avg height and mass for species # height by mass per species becomes new data frame mean_height_mass_by_species <- starwars %>% group_by(species) %>% summarise(average_height = mean(height, na.rm = TRUE), average_mass = mean(mass, na.rm = TRUE))

#tempdata is subset of starwars data pumped through operater to only keep listed varriables tempdata <- starwars %>% select(name, height, mass, gender, species)

#mutate function: aka add another column to existing table, use pipe to know what existing info we’re piping through bmidata <- tempdata %>% mutate(height1 = height/100) %>% #to calculate bmi, must convert height to from cm to m (above) and then preform nessasary calculations (below) mutate(BMI = mass / height1^2)

#filter function: aka narrow table/filter to population to one attribute (good for getting subset) bmidata_human <- bmidata %>% filter(species == “Human”) %>% #na.omit indicates that we’re omiting values that are not avaliable na.omit

#arrange function: sorts # desc aka descending order bmidata_human <- bmidata %>% filter(species == “Human”) %>% na.omit %>% arrange(desc(BMI))

#summarise: summarising the average bmi by gender via group_by function bmidata_human %>% group_by(gender) %>% #asking for mean (below) summarise(Average_BMI = mean(BMI, na.rm = TRUE))

#GENERAL NOTES _______________________________________________ #can add chunks by using green plus c (and then everything in between chunk is a comment, not # required) #can knit data to different files, makes it easy to send #rnotebook to summarise result