## ----intror:ggplot_weather, eval=FALSE----------------------------------- ## install.packages(c("ggplot2", "rattle")) ## library(ggplot2) ## library(rattle) ## qplot(data=weatherAUS, x=MinTemp, y=MaxTemp) ## ----intror:install.packages, eval=FALSE--------------------------------- ## # Install a package from a CRAN repository. ## ## install.packages(pkgs="dplyr") ## ----intror:data_glimpse------------------------------------------------- # Review the dataset. dplyr::glimpse(x=rattle::weatherAUS) ## ----intror:attach_dplyr_rattle, message=FALSE--------------------------- # Load packages from the local library into the R session. library(package=dplyr) library(package=rattle) ## ----intror:attach_dplyr_rattle_glimpse, message=FALSE------------------- # Review the dataset. glimpse(x=weatherAUS) ## ----intror:weather_glimpse, message=FALSE------------------------------- # Load packages from the local library into the R session. library(dplyr) library(rattle) # Review the dataset. glimpse(weatherAUS) ## ----intror:attach_packages, message=FALSE------------------------------- # Load packages required for this script. library(rattle) # The weatherAUS dataset and normVarNames(). library(readr) # Efficient reading of CSV data. library(dplyr) # Data wrangling and glimpse(). library(ggplot2) # Visualise data. library(magrittr) # Pipes %>%, %<>%, %T>%, %$%. ## ----intror:data_sum_function-------------------------------------------- # Add two numbers. sum(1, 2) ## ----intror:assignment_operator------------------------------------------ # Add two numbers and assign the result into a variable. v <- sum(1, 2) ## ----intror:assignment_results------------------------------------------- # Print the value stored in a variable. v print(v) ## ----intror:assignment_results_index------------------------------------- # Access a particular value from a vector of data. v[1] v[2] ## ----intror:data_library_command----------------------------------------- # Load package from the local library into the R session. library(rattle) ## ----intror:data_library_command_visible, out.lines=NULL----------------- # Demonstrate that library() returns a value invisibly. print(library(rattle)) ## ----intror:index_vector_library_command--------------------------------- # Load a package and save the value returned. l <- library(rattle) # Review one of the returned values. l[7] ## ----intror:data_plus_infix_function------------------------------------- # Add two numbers using an infix operator. 1 + 2 ## ----intror:data_plus_function------------------------------------------- # Add two numbers using the equivalent functional form. `+`(1, 2) ## ----intror:weather_print, out.lines=3----------------------------------- # Review the dataset of weather observations. weatherAUS ## ----intror:weather_select----------------------------------------------- # Select variables from the dataset. weatherAUS %>% select(MinTemp, MaxTemp, Rainfall, Sunshine) ## ----intror:weather_select_summary, out.lines=7-------------------------- # Select variables from the dataset and summarise the result. weatherAUS %>% select(MinTemp, MaxTemp, Rainfall, Sunshine) %>% summary() ## ----intror:weather_select_filter---------------------------------------- # Select specific variables and observations from the dataset. weatherAUS %>% select(MinTemp, MaxTemp, Rainfall, Sunshine) %>% filter(Rainfall >= 1) ## ----intror:weather_select_filter_assign_normal-------------------------- # Select variables/observations and save the result. rainy.days <- weatherAUS %>% select(MinTemp, MaxTemp, Rainfall, Sunshine) %>% filter(Rainfall >= 1) ## ----intror:weather_select_filter_assign--------------------------------- # Demonstrate use of the forward assignment operator. weatherAUS %>% select(MinTemp, MaxTemp, Rainfall, Sunshine) %>% filter(Rainfall >= 1) -> rainy.days ## ----intror:weather_select_filter_assign_unindent------------------------ # Best to unindent the final assigned variable for clarity. weatherAUS %>% select(MinTemp, MaxTemp, Rainfall, Sunshine) %>% filter(Rainfall >= 1) -> rainy.days ## ----intror:weather_select_filter_summary, out.lines=7------------------- # Summarise subset of variables for observations with rainfall. weatherAUS %>% select(MinTemp, MaxTemp, Rainfall, Sunshine) %>% filter(Rainfall >= 1) %>% summary() ## ----intror:weather_select_filter_summary_no_rain, out.lines=7----------- # Summarise observations with little or no rainfall. weatherAUS %>% select(MinTemp, MaxTemp, Rainfall, Sunshine) %>% filter(Rainfall < 1) %>% summary() ## ----intror:weather_select_filter_summary_no_rain_with_pipe, out.lines=7---- # Summarise observations with little or no rainfall. weatherAUS %>% select(MinTemp, MaxTemp, Rainfall, Sunshine) %>% filter(Rainfall < 1) %>% summary() ## ----intror:weather_select_filter_summary_no_rain_no_pipe, out.lines=7---- # Functional form equivalent to the pipeline above. summary(filter(select(weatherAUS, MinTemp, MaxTemp, Rainfall, Sunshine), Rainfall < 1)) ## ----intror:weather_assignment_pipe-------------------------------------- # Copy the dataset into the variable ds. ds <- weatherAUS # Report on the dimensions of the dataset. dim(ds) # Demonstrate an assignment pipeline. ds %<>% filter(Rainfall==0) %>% select(MinTemp, MaxTemp, Sunshine) # Confirm that the dataset has changed. dim(ds) ## ----intror:weather_assignment_no_pipe, eval=FALSE----------------------- ## # Functional form equivalent to the pipeline above. ## ## ds <- select(filter(weatherAUS, Rainfall==0), ## MinTemp, MaxTemp, Sunshine) ## ----intror:tee_pipe, out.lines=NULL------------------------------------- weatherAUS %>% select(Rainfall, MinTemp, MaxTemp, Sunshine) %>% filter(Rainfall==0) %T>% head() %>% select(MinTemp, MaxTemp, Sunshine) %>% summary() ## ----intror:tee_pipe_print----------------------------------------------- # Demonstrate usage of a tee-pipe. no.rain <- weatherAUS %>% filter(Rainfall==0) %T>% print() ## ----intror:tee_pipe_print_alternatives---------------------------------- # Equivalent alternatives to the above tee-pipe example. ( weatherAUS %>% filter(Rainfall==0) -> no.rain ) ## ----intror:tee_pipe_print_alternatives_print---------------------------- print(no.rain) ## ----intror:tee_pipe_usage, eval=FALSE----------------------------------- ## fpath <- "data" ## files <- dir(fpath, "*.csv.gz") ## ds <- data.frame() ## for (i in seq_len(length(files))) ## { ## ds <- fpath %>% ## file.path(files[i]) %T>% ## cat("\n") %>% ## readr::read_csv() %>% ## rbind(ds, .) ## } ## ----intror:magrittr_fly------------------------------------------------- # List the levels of a factor. levels(weatherAUS$WindGustDir) # weatherAUS %>% filter(Rainfall>0) %T>% { lvls <<- select(., WindGustDir, MaxTemp) %>% group_by(WindGustDir) %>% summarise(MaxMaxTemp=max(MaxTemp)) %>% arrange(MaxMaxTemp) %>% pull(WindGustDir) } %>% mutate(WindGustDir=factor(WindGustDir, levels=lvls)) %$% levels(WindGustDir) ## ----intror:exposition_pipe---------------------------------------------- weatherAUS %>% filter(Rainfall==0) %$% cor(MinTemp, MaxTemp) ## ----intror:exposition_pipe_alternative---------------------------------- weatherAUS %>% filter(Rainfall==0) %>% with(cor(MinTemp, MaxTemp)) ## ----intror:example_figure, eval=FALSE----------------------------------- ## cities <- c("Canberra", "Darwin", "Melbourne", "Sydney") ## ## weatherAUS %>% ## filter(Location %in% cities) %>% ## filter(Temp3pm %>% is.na() %>% not()) %>% ## ggplot(aes(x=Temp3pm, colour=Location, fill=Location)) + ## geom_density(alpha=0.55) + ## labs(title = "Density Distributions of the 3pm Temperature", ## x = "Temperature Recorded at 3pm", ## y = "Density") ## ----intror:example_figure, fig.pos='h', fig.height=3, fig.width=8, out.width="\\textwidth", echo=FALSE---- cities <- c("Canberra", "Darwin", "Melbourne", "Sydney") weatherAUS %>% filter(Location %in% cities) %>% filter(Temp3pm %>% is.na() %>% not()) %>% ggplot(aes(x=Temp3pm, colour=Location, fill=Location)) + geom_density(alpha=0.55) + labs(title = "Density Distributions of the 3pm Temperature", x = "Temperature Recorded at 3pm", y = "Density") ## ----intror:load_sa4_ports_data, echo=FALSE, message=FALSE--------------- avgdis <- read_csv("data/SA4_AvgDis_PoR_PoW.csv") ## ----intror:sydney_distance_travel, fig.pos='h', fig.height=4, fig.width=8, out.width="\\textwidth"---- avgdis %>% filter(GCCSA_CODE_2011=="1GSYD") %>% select(O_SA4name, O_Avg_KM, D_Avg_KM) %>% mutate(O_SA4name=str_replace(O_SA4name, "^Sydney - ", "")) %$% { nms <<- O_SA4name[order(O_Avg_KM)] ; . } %>% mutate(O_SA4name=factor(O_SA4name, levels=nms)) %>% gather("OD", "Distance", -O_SA4name) %>% arrange(O_SA4name) %$% { lbls <<- data.frame(x = sort(c(seq(0.75, 14.75), seq(1.2, 15.2))+0.02), y = (Distance-0.75), lbl = round(Distance,1)) ; . } %>% ggplot(aes(x=O_SA4name, y=Distance)) + geom_bar(stat="identity", position="dodge", aes(fill=OD)) + geom_text(data=lbls, aes(x=x, y=y, label=lbl), colour="white", size=3, hjust=0.5) + theme(axis.text.x=element_text(angle=45, hjust=1, size=10)) + theme(legend.position=c(.15, .9)) + scale_fill_manual(values=c(rgb(246, 160, 26, max=255), rgb( 0, 101, 164, max=255)), labels=c("By SA4 of residence", "By SA4 of work")) + theme(legend.title=element_text(colour="transparent"), legend.background=element_rect(fill="transparent"), legend.direction="horizontal") + labs(x="Sydney", y="Average Distance (KM)") ## ----intror:tee_pipe_id_1------------------------------------------------ weatherAUS %>% select(Rainfall, MinTemp, MaxTemp, Sunshine) %>% {.} ## ----intror:tee_pipe_id_2, out.lines=NULL-------------------------------- weatherAUS %>% select(Rainfall, MinTemp, MaxTemp, Sunshine) %>% summary() %>% {.}