Skip to content

Instantly share code, notes, and snippets.

# problem 1
# part 1
pollutantmean <- function(directory, pollutant, id = 1:332) {
filelist <- list.files(path = directory, pattern = ".csv",
full.names = TRUE)
values <- numeric()
for(i in id) {
@byanuaria
byanuaria / Assignment3.R
Created June 6, 2018 23:07
John Hopkins Introduction to Programming Assignment 3
library(tidyverse)
# read in the data
outcome <- read.table("rprog_data_ProgAssignment3-data/outcome-of-care-measures.csv",
header = TRUE, sep = ",", colClasses = "character")
nrow(outcome)
ncol(outcome)
names(outcome)
@byanuaria
byanuaria / Assignment3.2.R
Created June 7, 2018 17:24
3 functions to find the hospital with the best rates for heart attacks, heart failures, and pneumonia
## find the hospital for state with best heart attack rate
ha <- function(thestate) {
data <- read.csv("outcome-of-care-measures.csv",
header = TRUE, sep = ",",
colClasses = "character")
# convert to proper class
data[,11] <- as.numeric(data[,11])
for(i in thestate) {
@byanuaria
byanuaria / Assignment3.3.R
Last active June 7, 2018 21:48
Almost there, but it returns the underlying code of characters instead of the actual numerical value
best <- function(thestate, outcome) {
mydata <- read.csv("outcome-of-care-measures.csv", header = TRUE, sep = ",",
colClasses = "character")
for(i in thestate) {
if(outcome == "heart attack") {
mystate <- subset(mydata, State = thestate, select = c(2, 7, 11))
ind <- which.min(mystate[, 3])
myhospital <- mystate[ind, 1]
@byanuaria
byanuaria / sqlzooJOIN.sql
Created July 17, 2018 20:03
SQLzoo JOIN assignments
/* Show matchid and player name for all goals scored by Germany */
SELECT matchid, player
FROM goal
WHERE teamid = 'GER'
/* Show id, stadium, team1, team2 for just game 1012 */
SELECT id, stadium, team1, team2
FROM game
WHERE id = 1012
/* More JOIN */
/* Give year of Citizen Kane */
SELECT yr
FROM movie
WHERE title = 'Citizen Kane'
/* List all of the Star Trek movies, include the id, title and yr (all of these movies include the words Stark Trek in the title). Order results by year */
SELECT id, title, yr
FROM movie
WHERE title LIKE 'Star Trek%'
@byanuaria
byanuaria / sqlzooNULL.sql
Created July 18, 2018 18:18
sqlzoo assignment working with joins and NULL values.
/* Using NULL */
/* List the teachers who have NULL for their department */
SELECT name
FROM teacher
WHERE dept IS NULL
/* Use a different JOIN so that all teachers are listed */
SELECT teacher.name, dept.name
FROM teacher LEFT JOIN dept ON teacher.dept = dept.id
@byanuaria
byanuaria / sqlzooSelfJOIN.sql
Created July 18, 2018 20:19
self join section from sqlzoo
/* Self join * /
/* How many stops are in the database */
SELECT COUNT(*)
FROM stops
/* Find the id value for the stop Craiglockhart */
SELECT id
FROM stops
WHERE name = 'Craiglockhart'
/* How many albums does the artist Led Zepplin have? */
SELECT COUNT(Albumid) AS totalAlbums
FROM albums
WHERE Artistid = (SELECT Artistid FROM artists WHERE name = 'Led Zeppelin')
/* Create a list of album titles and the unit prices for the artist Audioslave */
SELECT n.Name, u.UnitPrice
FROM ((albums t INNER JOIN artists n
ON t.Artistid = n.Artistid)
INNER JOIN tracks u ON t.Albumid = u.Albumid)
@byanuaria
byanuaria / commaCode.py
Created July 27, 2018 20:07
Automate the boring stuff pratice project: Comma Code. A function that takes a list value as an argument and returns a string with all the items seperated by a comma and a space, with 'and' inserted before the last item.
# lists to test
foo = ['apples', 'bananas', 'tofu', 'cats']
a = ['bat', 'rat', 'elephant', 'snake', 'hippo']
b = ['Ben', 'Ryan', 'Arthur', 'Dillon', 'Andre', 'Agnes', 'Aaron']
d = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
# comma code function
def commaCode(myList):
s = '' # creates an empty string vector
last = myList[len(myList) - 1] # gets the last value from the list