# Example R code
define_my_median <- function(vector) {
# Code logic here
return(quantile(vector, probs = 0.5, na.rm = TRUE))
}Lab 02
Due Jan 31 5:00 PM PST
Instructions
For this homework, provide a rendered R Markdown file in PDF format. You may render the R Markdown file to HTML and then convert the HTML file to PDF using the print function in a web browser. Indicate your student number on the markdown file and make a section for each problem.
Problem 1: My Median
- Write an R function called
my_median. This function should take a single parameter (a vector) and return the median of that vector. Write this function without using the built-in R commandmedian. Provide the code for this function.
(4 points)
- Consider the stock prices in the file
AS-N100.tsvprovided in the archive for week 3. These data involve stock prices for 27 symbols (in the columnticker). Compute your student number modulo 27 and add one, call this valuea(i.e.,ais a number between 1 and 27 inclusive). Imagine a list of the 27 symbols in alphabetical order. You will examine thea-th symbol in this list. You may find the symbol using the R codesort(unique(data$ticker))[a]. Compute the median opening price of the symbol you examine over all rows inAS-N100.tsvinvolving that symbol, using yourmy_medianfunction. Provide all code and output.
(2 points)
## this code doesn't run at the moment, need to modify it
# Example R code to read and process
library(readr)
data <- read_tsv("AS-N100.tsv", show_col_types = FALSE)
symbols <- sort(unique(data$ticker))
# Compute value of 'a'
a <- (301623300 %% 27) + 1
a
symbols <- sort(unique(data$ticker))[a]
# Example of custom median application
my_median <- define_my_median(subset(data, ticker == symbols)$open)
my_medianProblem 2: X Marks the Spot
- The image
Figure03.png(provided in the archive containing this lab) is a satellite photo of Vancouver and the surrounding area. In this problem, you will mark the location of a library on this image automatically using code.
The upper left-hand corner of this image has approximate GPS coordinates 49.410705,-124.217671. The lower right-hand corner of this image has approximate GPS coordinates 47.929083,-121.994887. Consider the file libraries.json provided in the archive for week 3. This file contains information about 21 branches of the Vancouver Public Library.
Write R code to display Figure03.png and to mark an X symbol on the GPS coordinates of the b-th library branch. Print the name and address of the library branch directly below the X. Here, b is your student number modulo 21 plus one (i.e., b is a number between 1 and 21 inclusive).
Note that after loading the library rjson and loading libraries.json using:
libraries <- fromJSON(file = "libraries.json")the coordinates of the b-th branch can be accessed with:
libraries[["features"]][[b]][["geometry"]]$coordinatesThe branch name and address can be accessed with:
libraries[["features"]][[b]][["properties"]]$maptipProvide your code and the resulting display.
(4 points)
## this code doesn't run at the moment, need to modify it
# Example R code for the library plot
library(rjson)
library(imager)
library(ggplot2)
library(png)
library(grid)
libraries <- fromJSON(file = "libraries.json")
# Additional code to process and display
b <- (301623300 %% 21) + 1
b
coordinates <- libraries[["features"]][[b]][["geometry"]]$coordinates
maptip <- libraries[["features"]][[b]][["properties"]]$maptip
upper_left <- c(49.410705, -124.217671)
lower_right <- c(47.929083, -121.994887)
img_width <- 8094
img_height <- 8094
x_pixel <- img_width * (coordinates[1] - upper_left[2]) / (lower_right[2] - upper_left[2])
y_pixel <- img_height * (upper_left[1] - coordinates[2]) / (upper_left[1] - lower_right[1])
image <- load.image("Figure03.png")
plot <- ggplot() +
annotation_custom(rasterGrob(image, width = unit(1, "npc"), height = unit(1, "npc")))+
geom_point(aes(x = x_pixel, y = y_pixel), color = "red", size = 3) +
annotate("text", x = x_pixel, y = y_pixel - 20, label = maptip, color = "blue", size = 4, hjust = 0.5) +
xlim(0, img_width) +
ylim(0, img_height) +
theme_void()
print(plot)