@kmcdougald

# Calculate a given annual turnover (%) and 10- and 15-year survival rates (%) given a median tenure length in years

mtl <- 7.5 # Input: Median tenure length
atr <- (0.523*(mtl^-0.917)) # Calculate: Estimated annual turnover rate
survi10 <- 1.1111*exp(-11.87*atr) # Calculate: Estimated 10-year survival rate
survi15 <- 1.1822*exp(-18.04*atr) # Calculate: Estimated 15-year survival rate

paste("Median tenure length: ", mtl)
paste("Estimated annual turnover rate: ", atr)
paste("Estimated 10-year 

IP Address Finder

updated November 25, 2022 · Python
def checkforip():
    import socket
    hostname = socket.gethostname()
    IPAddr = socket.gethostbyname(hostname)
    return(print("Host: " + hostname + " IP:" + IPAddr))


checkforip()

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("169.254.186.61", 80))
print(s.getsockname()[0])
s.close()

Password Creator

updated November 25, 2022 · Python
# PURPOSE: TO GENERATE A LIST OF PASSWORDS WHEN YOU NEED TO CREATE A NEW ONE -- PICK WHICHEVER ONE LOOKS BEST
# ADJUST y TO CREATE A LONGER OR SHORTER LIST
# ADJUST masterlist TO ALTER TYPES OF CHARACTERS USED IN PASSWORD

def passwordcreator():
    import random
    password = []
    masterlist = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n'

Viewer preference exercise

September 15, 2022 · R
# Exercise: Determine viewer preferences pertaining to TV news anchor, newscast time and duration
# IDENT = Case ID, ANCHOR = Preferred Anchor, TIME = Preferred Time, DURATION = Preferred Duration

IDENT <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50)
ANCHOR <- c(3,3,2,2,3,2,3,2,1,2,1,1,3,2,2,1,3,3,1,1,3,3,3,1,2,1,3,1,1,3,3,3,1,1,3,3,3,3,2,2,2,3,1,3,2,3,3,1,2,1)
TIME <-c(2,3,3,3,3,1,1,2,3,1,2,2,1,

Regression exercise

updated September 10, 2022 · R
# Estimate the year in which memberships and confirmations reach zero
# Based on published Anglican Church of Canada data 

year <- c(2017,2000,1995,1990,1985,1980,1975,1970)
membership <- c(359,651,740,813,865,930,1015,1127) # In thousands, rounded to nearest
confirms <- c(1997,5525,7183,8931,13341,15057,19653,21683) # Actual number

# Create a table and dataframe based on the above
datatable <- array(c(year,membership,confirms),dim=c(8,3))
datatable

df <- data.frame(datatable)

# Calculate y

FCC TV Query Database Data

updated September 09, 2022 · SQL
CREATE TABLE Stations (
    CALLSIGN varchar(255),
    RFCHANNEL int,
    VIRTUALCHANNEL int,
    SERVICE varchar(255),
    OPSTATUS varchar(255),
    CITY varchar(255),
    PROV varchar(255),
    COUNTRY varchar(255),
    DISTANCEKM double(8,2),
    DISTANCEMI double(8,2),
    POWERKW double(8,2),
    HAAT double(8,2),
    LATITUDE double(8,4),
    LONGITUDE double(8,4),
    ASRN int,
    LICENCEE varchar(255));

INSERT INTO Stations VALUES('CKYB-TV',4,NULL,'DTV','LIC','BRANDON','MB','CA',207.
# USER INPUTS
RangeToCover <- 104.78 # Distance in kilometres
paste("Range specified: ",RangeToCover, " km")
TxSiteElev <- 244.1 # Tx site elevation in metres above sea level
paste("Transmitter site elevation specified: ", TxSiteElev, " m")
MaxTerrain <- 253 # Maximum intervening terrain in metres a.s.l.
paste("Maximum terrain to overcome specified: ", MaxTerrain, " m")
RxAntElev <- 9 # Receiving antenna elevation in metres above ground
paste("Receiving antenna elevation specified: ", RxAntElev

Coverage range calculator

updated September 11, 2022 · Python
# Calculate theoretical coverage area for a broadcast
import math
txheight = 542.2 # Height in metres above maximum terrain
rxheight = 9.14 # Height in metres above maximum terrain
if txheight > 0 and rxheight > 0:
    contour1 = (3.57*math.sqrt(txheight)) + (3.57*math.sqrt(rxheight))
    contour2 = (3.57*math.sqrt(txheight * 1.333)) + (3.57*math.sqrt(rxheight*1.333))
    contour3 = (2*math.sqrt(8*txheight)) + (2*math.sqrt(8*rxheight))
    print("Best coverage without relying on diffraction -- 

Canadian air travel meltdown 2022

updated July 07, 2022 · R
# ASSEMBLE THE DATA
AIRLINE <- c('WestJet','Air Canada Express','Swoop','Porter Airlines','WestJet','WestJet','Porter Airlines','Air Canada Express','Air Canada Express','WestJet','Swoop','Porter Airlines','Porter Airlines','Air Canada Express','WestJet','Porter Airlines','Air Canada','WestJet','WestJet','Porter Airlines','Porter Airlines','Air Canada Express','Porter Airlines','Porter Airlines','Porter Airlines','Air Canada','WestJet','WestJet','WestJet','Air Canada Express','Porter Airlines',

Simulated Election Poll

updated July 11, 2022 · R
# BASIC SETUP AREA

# RESPID - Respondent ID.
RESPID <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,1

Airline Flight Scheduler

updated September 27, 2022 · Python
# The challenge: Recreate North Central Airlines as a post-deregulation network.
# Convairs replaced with Fokker F28s.
# To switch to F28 feeder network, replace "topmarkets" with "mspfringe" or "dtwfringe" twice in the 200s and
# change turnaround time to 40 minutes.
# Also, change focuscity setting below.


# Imports
import random
import math

# Set fixed items
focuscity =  "DTW" # Change as needed
hubs = ["MSP", "DTW"]
topmarkets = ["JFK", "LAX", "ORD", "PHL", "SFO", "BOS", "DTW", "DCA", "CL
# Import variables
Air_pollution <- c(6.7,12.2,12.8,10.1,23.4,22.6,17,12,10,10,5.9,5.5,11.4,10.1,14.5,16.7,7.8,6.4,19.7,15.9,13.7,27.3,10.5,10,12.7,12.2,6.7,22.8,8.3,18.5,17,5.8,7.7)

Dwellings_without_basic_facilities <- c(NA,0.8,0.7,0,9.4,12.3,0.5,0.1,0.5,0.3,5.7,0.4,0.5,0.5,0.4,3.5,0.2,0,NA,0.6,6.4,2.5,11.8,0.1,11.2,0.1,0,2.3,0.9,1.5,0.2,0,0.1)

Educational_attainment <- c(84,85.6666666666667,79.6666666666667,89,67.3333333333333,58.6666666666667,94,86.3333333333333,81.6666666666667,63,90.666
# All data used here is from a publicly available 2007 telephone survey data file on Canadian home ownership issues.
# Create data.
Jz8 = [3,4,1,1,1,1,2,1,1,2,1,1,0,4,5,1,1,4,1,1,1,6,4,1,1,1,1,2,1,1,1,1,1,1,1,1,2,1,1,1,4,1,1,1,1,1,1,7,1,1,0,1,1,2,1,1,1,1,1,1,1,1,1,1,2,1,8]
Jz11 = [2000,300000,30000,50000,10000,10000,10000,40000,25000,25000,15000,350000,0,100,50000,25000,50000,10000,8500,150000,150000,5000,20000,7500,20000,25000,10000,100000,25000,5000,50000,30000,5000,15000,50000,50000,30000,30

Exercises: Survey Data Analysis in R

updated April 01, 2022 · R
ident <- c(1888808,1363621,2269327,1568468,2172194,2830017,2727534,2727611,419204,1980519,2508601,2928347,2381052,2146554,2802741,85655,1594857,2765410,1250741,2280287,1878565,1190122,2086837,2917430,1747124,1269092,2846200,1297840,396630,2248729,1230161,2913173,80897,2943499,295162,2951686,2668518,2031662,2786557,2863317,2167337,2207390,2216281,2748903,2143115,274505,1222442,2749016,2665448,1622899,1233983,2727926,2932583,2867360,692105,529978,1410014,2722209,2959417,2929037,2760749,1590653,28

Cluster Analysis

updated October 28, 2022 · R
# Purpose of program: Identify approximate 'hub' locations.

latitude <- c(51.68193889,48.99751944,53.87039444,46.50085,48.13558889,50.17194722,50.16246667,49.35085556,52.53748056,55.66825,49.009625,50.17194722,48.42136389,53.82780833,50.64528333,54.44206111,51.08060833,44.65092778,48.26415,50.44973611,50.64849722,45.17642778,48.54959444,49.23361944,49.39107222,48.42136389,53.20140833,48.13554167,50.59556667,49.39111667,54.25595833,50.67071389,54.78813056,48.26415,53.53191389,43.04615833,49.779