diff --git a/exercise-1/exercise.R b/exercise-1/exercise.R index 47668aa..776be2b 100644 --- a/exercise-1/exercise.R +++ b/exercise-1/exercise.R @@ -2,20 +2,23 @@ # Create a vector of the number of points the Seahawks scored the first 4 games of last season # Hint: (google "Seahawks scores 2016") +points <- c(17, 11, 27, 23) # Create a vector of the number of points the Seahawks have allowed to be scored against them in the first 4 games - +points.against <- c(16, 18, 17, 21) # Combine your two vectors into a dataframe - +seahawks.points <- data.frame(scored = points, allowed = points.against) # Create a new column "diff" that is the difference in points - +seahawks.points$diff <- seahawks.points$scored - seahawks.points$allowed # Create a new column "won" which is TRUE if the Seahawks wom - +seahawks.points$won <- seahawks.points$diff > 0 # Create a vector of the opponents - +opponents <- c("Chiefs", "Vikings", "Cowboys", "Raiders") # Assign your dataframe rownames of their opponents +rownames(seahawks.points) <- opponents + diff --git a/exercise-2/Salary Table.csv b/exercise-2/Salary Table.csv new file mode 100644 index 0000000..78e56e1 --- /dev/null +++ b/exercise-2/Salary Table.csv @@ -0,0 +1,101 @@ +"","old.salaries","new.salaries","raise","got.raise" +"Employee 1",15471.5831857175,24660.3985223919,9188.81533667445,TRUE +"Employee 2",17024.5250617154,22619.9621916749,5595.43712995946,TRUE +"Employee 3",16939.8295274004,18340.9336069599,1401.10407955945,TRUE +"Employee 4",16430.3230843507,20935.9419951215,4505.61891077086,TRUE +"Employee 5",18829.260522034,22251.7891740426,3422.52865200862,TRUE +"Employee 6",16310.5632923543,15497.2274065949,-813.335885759443,FALSE +"Employee 7",17703.4537214786,22104.3024049141,4400.84868343547,TRUE +"Employee 8",12744.5594174787,15547.3345285282,2802.77511104941,TRUE +"Employee 9",13671.6858367436,23223.5220680013,9551.83623125777,TRUE +"Employee 10",15839.6912622266,19864.8738395423,4025.18257731572,TRUE +"Employee 11",11288.8949434273,24959.7517028451,13670.8567594178,TRUE +"Employee 12",11594.6648595855,20591.3114268333,8996.64656724781,TRUE +"Employee 13",18673.4580039047,16161.9795695879,-2511.47843431681,FALSE +"Employee 14",13468.250578735,15806.9349289872,2338.68435025215,TRUE +"Employee 15",15120.7372592762,23442.5970050506,8321.85974577442,TRUE +"Employee 16",19259.9176662043,15424.1305869073,-3835.78707929701,FALSE +"Employee 17",10568.9407000318,21373.7490680069,10804.8083679751,TRUE +"Employee 18",13217.6828407682,19925.2308881842,6707.54804741591,TRUE +"Employee 19",12906.7244171165,15750.4430948757,2843.71867775917,TRUE +"Employee 20",19246.8270030804,21959.8895078525,2713.0625047721,TRUE +"Employee 21",12090.5151590705,17109.2880284414,5018.77286937088,TRUE +"Employee 22",15994.7342309169,19039.7099801339,3044.97574921697,TRUE +"Employee 23",16787.5963891856,20636.9690410793,3849.37265189365,TRUE +"Employee 24",17197.1589722671,22587.263637688,5390.10466542095,TRUE +"Employee 25",10755.3937286139,15337.8674597479,4582.47373113409,TRUE +"Employee 26",16389.9164530449,16417.115367949,27.1989149041474,TRUE +"Employee 27",18243.7092927285,21122.6525506936,2878.94325796515,TRUE +"Employee 28",19739.1754062846,24687.8592786379,4948.68387235329,TRUE +"Employee 29",10039.1245284118,18395.7235934213,8356.5990650095,TRUE +"Employee 30",10008.7988935411,17777.5453682989,7768.74647475779,TRUE +"Employee 31",19313.6189621873,19873.0512079783,559.432245790958,TRUE +"Employee 32",11045.1733809896,22356.4107506536,11311.237369664,TRUE +"Employee 33",16301.124272868,21375.6613037549,5074.53703088686,TRUE +"Employee 34",11589.5037492737,16354.0025544353,4764.49880516157,TRUE +"Employee 35",12689.799531363,15793.6811656691,3103.88163430616,TRUE +"Employee 36",17753.3540735021,22160.0559842773,4406.70191077515,TRUE +"Employee 37",19078.074619174,17305.833355058,-1772.24126411602,FALSE +"Employee 38",17021.5832791291,23549.7916047461,6528.20832561702,TRUE +"Employee 39",15288.8093935326,24099.6629046276,8810.85351109505,TRUE +"Employee 40",10304.6791977249,19778.4959431738,9473.81674544886,TRUE +"Employee 41",17738.4423930198,17734.5830481499,-3.85934486985207,FALSE +"Employee 42",17400.2753896639,19136.2607385963,1735.98534893245,TRUE +"Employee 43",19821.8254302628,17032.4164163321,-2789.40901393071,FALSE +"Employee 44",14110.5442144908,19025.2866409719,4914.7424264811,TRUE +"Employee 45",13637.7344070934,19989.9465637282,6352.21215663478,TRUE +"Employee 46",13154.2299361899,17060.6262073852,3906.39627119526,TRUE +"Employee 47",14950.2341938205,19888.124007266,4937.88981344551,TRUE +"Employee 48",14311.7657047696,23272.5309138186,8960.76520904899,TRUE +"Employee 49",13494.4648714736,19221.5843708254,5727.11949935183,TRUE +"Employee 50",12303.6432615481,17886.0702784732,5582.42701692507,TRUE +"Employee 51",17453.3595121466,17881.9988574833,428.639345336705,TRUE +"Employee 52",16053.5358567722,21526.6355616041,5473.09970483184,TRUE +"Employee 53",15215.8143976703,16710.570610594,1494.75621292368,TRUE +"Employee 54",16956.6500768997,22565.2057747357,5608.55569783598,TRUE +"Employee 55",13789.8462545127,16056.1306541786,2266.28439966589,TRUE +"Employee 56",16609.087085817,21201.1836073361,4592.09652151912,TRUE +"Employee 57",11684.6466809511,19834.4825487584,8149.83586780727,TRUE +"Employee 58",13350.3392292187,18659.0060754679,5308.66684624925,TRUE +"Employee 59",13188.7284107506,21388.861106243,8200.13269549236,TRUE +"Employee 60",17816.4206095971,24053.3618535846,6236.94124398753,TRUE +"Employee 61",10272.4001021124,24316.3373670541,14043.9372649416,TRUE +"Employee 62",10950.2792870626,16160.290860571,5210.01157350838,TRUE +"Employee 63",11217.5932270475,20422.052978538,9204.4597514905,TRUE +"Employee 64",13615.8408503979,17698.8480775617,4083.00722716376,TRUE +"Employee 65",18428.6481468007,24159.2815122567,5730.63336545601,TRUE +"Employee 66",17269.5621964522,24110.9416633844,6841.37946693227,TRUE +"Employee 67",14536.723876372,19841.7027899995,5304.97891362756,TRUE +"Employee 68",14706.3061920926,16961.6439496167,2255.3377575241,TRUE +"Employee 69",15602.2931355983,20928.6237484775,5326.33061287925,TRUE +"Employee 70",16864.9258720689,23921.5546916239,7056.62881955504,TRUE +"Employee 71",13410.5371404439,21734.3316646293,8323.79452418536,TRUE +"Employee 72",15764.4355297089,20047.0235571265,4282.58802741766,TRUE +"Employee 73",13263.6101637036,15552.2036529146,2288.59348921105,TRUE +"Employee 74",15549.847104121,20904.4301812537,5354.5830771327,TRUE +"Employee 75",12055.4679306224,23384.900491219,11329.4325605966,TRUE +"Employee 76",19631.3081472181,18471.8029829673,-1159.50516425073,FALSE +"Employee 77",17659.1643691063,21103.6703502759,3444.50598116964,TRUE +"Employee 78",18986.8946489878,24246.846770402,5259.95212141424,TRUE +"Employee 79",18530.7291825302,16728.2229335979,-1802.50624893233,FALSE +"Employee 80",17118.4929460287,19619.0372342244,2500.54428819567,TRUE +"Employee 81",16635.8638578095,24673.4839631245,8037.620105315,TRUE +"Employee 82",17129.5867930166,20903.2829920761,3773.69619905949,TRUE +"Employee 83",12400.6685335189,15323.6153698526,2922.94683633372,TRUE +"Employee 84",10928.5174915567,16406.0634025373,5477.54591098055,TRUE +"Employee 85",17173.5368506052,23974.1721958853,6800.63534528017,TRUE +"Employee 86",15431.5356654115,17028.2800775021,1596.74441209063,TRUE +"Employee 87",10476.037801709,19220.8513850346,8744.81358332559,TRUE +"Employee 88",15862.92659631,22339.7839674726,6476.85737116262,TRUE +"Employee 89",14144.1961959936,22639.0363974497,8494.8402014561,TRUE +"Employee 90",13673.1778178364,21139.5580694079,7466.38025157154,TRUE +"Employee 91",17314.8568533361,20268.3355845511,2953.478731215,TRUE +"Employee 92",18290.6822767109,20079.8264984041,1789.14422169328,TRUE +"Employee 93",16449.3037760258,18336.3306662068,1887.02689018101,TRUE +"Employee 94",16164.8999387398,19420.5962936394,3255.69635489956,TRUE +"Employee 95",16031.6081950441,23196.6026383452,7164.99444330111,TRUE +"Employee 96",13125.5948147736,17121.3754313067,3995.78061653301,TRUE +"Employee 97",19090.5168978497,16035.3372828104,-3055.17961503938,FALSE +"Employee 98",14539.1377666965,18011.9625944644,3472.82482776791,TRUE +"Employee 99",19878.3967015333,15699.8622533865,-4178.53444814682,FALSE +"Employee 100",17396.611708682,22625.2407813445,5228.62907266244,TRUE diff --git a/exercise-2/exercise.R b/exercise-2/exercise.R index aec2e48..4204138 100644 --- a/exercise-2/exercise.R +++ b/exercise-2/exercise.R @@ -1,51 +1,54 @@ # Create a vector of 100 employees ("Employee 1", "Employee 2", ... "Employee 100) - +employees <- c(paste("Employee ", 1:100)) # Create a vector of 2014 salaries using the runif function - +old.salaries <- runif(100, 10000, 20000) # Create a vector of 2015 salaries that are typically higher than the 2014 salaires (use runif again) - +new.salaries <- runif(100, 15000, 25000) # Create a data.frame 'salaries' by combining the vectors you just made - +salaries <- data.frame(old.salaries, new.salaries) +row.names(salaries) <- employees # Create a column 'raise' that stores the size of the raise between 2014 and 2015 - +salaries$raise <- new.salaries - old.salaries # Create a column 'got.raise' that is TRUE if the person got a raise - +salaries$got.raise <- salaries$raise > 0 # Retrieve values from your data frame to answer the following questions: # What was the 2015 salary of employee 57 - +salaries$new.salaries[[57]] # How many employees got a raise? - +table(salaries$got.raise)["TRUE"] # What was the value of the highest raise? - +salaries$raise[which.max(salaries$raise)] # What was the name of the employee who recieved the highest raise? - +row.names(salaries)[which.max(salaries$raise)] # What was the largest decrease in salaries between the two years? - +salaries$raise[which.min(salaries$raise)] # What was the name of the employee who recieved largest decrease in salary? - +row.names(salaries)[which.min(salaries$raise)] # What was the average salary increase? - +mean(salaries$raise) ### Bonus ### # Write a .csv file of your salaries to your working directory - +write.csv(salaries, file = "Salary Table.csv", ) # For people who did not get a raise, how much money did they lose? - +loss <- !salaries$got.raise +loss.salaries <- salaries$raise[loss] +mean(loss.salaries) # Is that what you expected them to lose? diff --git a/exercise-3/exercise.R b/exercise-3/exercise.R index c33c9cd..d6ecaf6 100644 --- a/exercise-3/exercise.R +++ b/exercise-3/exercise.R @@ -1,17 +1,17 @@ # Exercise 3: Working with Data Frames # Load R's "USPersonalExpenditure" dataest using the `data()` function - +data("USPersonalExpenditure") # The variable USPersonalExpenditure is now accessible to you. Unfortunately, it's not a data.frame # Test this using the is.data.frame function - +is.data.frame(USPersonalExpenditure) # Luckily, you can simply pass the USPersonalExpenditure variable to the data.frame function # to convert it a data.farme # Create a new variable by passing the USPersonalExpenditure to the data.frame function - +my.data <- data.frame(USPersonalExpenditure) # What are the column names of your dataframe? @@ -23,24 +23,27 @@ # Create a column `category` that is equal to your rownames - +my.data$category <- row.names(my.data) # How much money was spent on personal care in 1940? - +my.data["Personal Care", 'X1940'] # How much money was spent on Food and Tobacco in 1960 - +my.data$X1960[1] # What was the highest expenditure category in 1960? - +my.data$category[which.max(my.data$X1960)] ### Bonus ### # Write a function that takes in a year as a parameter, and # returns the highest spending category of that year +HighestSpending <- function(year) { + return (my.data$category[which.max(my.data[, paste0("X", year)])]) +} # Using your function, determine the highest spending category of each year - +HighestSpending(1940) # Write a loop to cycle through the years, and store the highest spending category of # each year in a list diff --git a/exercise-4/exercise.R b/exercise-4/exercise.R index 998ab3a..1e0b809 100644 --- a/exercise-4/exercise.R +++ b/exercise-4/exercise.R @@ -2,34 +2,36 @@ # Read data into a variable called `grants` using the `read.csv` function # Make sure *not* to read the strings as factors +grants <- read.csv("data/gates_money.csv", stringsAsFactors = FALSE) # Use the View function to look at your data - +View(grants) # Create a variable `spending` as the `total_amount` column of the dataset - +spending <- grants$total_amount # Confirm that your `spending` variable is a vector using the `is.vector` function - +is.vector(spending) # Create a variable `org` as the `organization` column of the dataset. - +org <- grants$organization ### Now you can ask some more interesting questions about the dataset. Store your answers in variables ### # What was the mean grant value? - +mean(spending) # What was the dollar amount of the largest grant? - +spending[which.max(spending)] # What was the dollar amount of the smallest grant? - +spending[spending == min(spending)] # Which organization received the largest grant? - +org[spending == max(spending)] # Which organization received the smallest grant? +org[spending == min(spending)] - -# How many grants were awarded in 2010? \ No newline at end of file +# How many grants were awarded in 2010? +length(spending)