00:01In this video, I'm going through the answers to the exercises from Chapter 3 on Data Manipulation.
00:08You'll see on the bottom of my screen any keyboard shortcuts that I press.
00:18The most useful keyboard shortcuts that I use are Control 1 and Control 2,
00:25which switch between the R console and the actual notebook, which is being edited,
00:33as well as Control Alt plus I in order to create notebook chunks.
00:40And then once I'm inside of a notebook chunk, Control Shift Enter will run the code in that chunk.
00:48So to start, I'm going to load the exam PA data library as well as the tidyverse library.
01:00And here the data is called actuary salaries.
01:08If I wanted to look at this data entirely, I could use a pipe and then view with an uppercase
01:18V.
01:19And here you can see all of the records.
01:22You can even filter to specific values.
01:31The glimpse function will give you a summary.
01:35There are six variables, 138 observations.
01:41There are rows for industry, exams, experience, salaries.
01:45Now this is the data from the D.W. Simpson actuary salary survey, which you may be familiar with.
01:54Question 2.
01:55The salary high column is a character type when it should be numeric.
02:00Change this to numeric.
02:01So I want to mutate the data and then save it as a new version.
02:11And I want to mutate salary high so that it is a numeric character of salary high.
02:29Question 3.
02:33What are the highest and lowest salaries for an actuary in health with five exams passed?
02:42So I want to mutate, I want to filter so that industry is equal to health and exams is equal
02:59to five.
03:09and that will give me four records and you can see the highest salary is 126
03:16and the lowest is 68 so if I want to summarize I want to use the summarize
03:24function and say high is equal to the max salary high low is equal to the min of
03:39salary assuming that I can spell correctly 126 to 68 question four create a
03:53new column called salary mid which is the middle of salary low and salary high so I
04:04want the I just realized that my keyboard shortcuts are no longer working or no
04:17longer showing up appropriately that's okay salary so create a new column called
04:25salary mid so I want to mutate salary mid is equal to the midpoint salary low plus
04:40salary high over two and I want to save this as the original data object
04:56question five when grouping by industry what is the highest salary mid keyword is grouping so I want
05:06to group by industry and then summarize so that's highest salary mid so I salary
05:19mid is equal to the max salary mid is equal to the max salary mid and the high salary high
05:32is equal to
05:37max salary high and low salary low is equal to the min salary low
05:52low and you can see here for casualty actually raise this is saying that the low salary is 11 which
06:02is of course far too low which is what question six is referring to
06:08it says there is a mistake when salary low is equal to 11 find and fix this mistake and then
06:14run the code from the previous tasks
06:17so I want to find the row in the original data where the salary low is equal to 11 I
06:25could just look at this data here
06:30another shortcut if you just click on an object and press F2 it will view that object
06:38so here you I could just you know filter salary high or salary low and see that the low is
06:4811 and then find this row if I want to do that directly I can just use the filter
07:04filter
07:05filter
07:07there
07:11there's that row
07:16so if I want to find and fix this mistake so the salary range really should be 114 to 166
07:23but it's showing up as 11 to 66
07:27So to correct this, I want to mutate both of these columns, salary low and salary high.
07:37So I want salary low is equal to if-else, salary low is equal to 11.
07:46If that's the case, I want it to be 114.
07:51Otherwise, keep it as it is.
07:56Salary high is, if salary high is equal to 66, it should be 166.
08:11Go back and save that as the object.
08:16Question 7.
08:19Create a new column called N-exams, which is an integer.
08:23Use 7 for ASA or ACAS and 10 for FSA or FCAS.
08:28Use the code below as a starting point.
08:36So this is a complicated function.
08:41Whenever I don't know what a function is doing, I like to look at the documentation.
08:47The way to do that is to type in question mark and then the name of the function.
08:52Here you can see some examples.
08:59So it takes in a boolean expression, which is right here.
09:05And then based on that, it returns a result if it's true.
09:13So if exams is equal to FSA, use 10, FASA 7, FCAS use 10, ACAS use 7.
09:28And then for all other values, in other words, which this last expression is always true,
09:38use this substring of the exams column.
09:42If you look at the exams column, you can see that sometimes it's a number.
09:47Other times it has the word exam afterwards.
09:51So for these records, just take the first character.
09:56So if you look at the substring documentation,
10:03you'll see that it takes a character vector and then a starting value and a stopping value.
10:08So this is the exams column as the character.
10:12And the starting value is 1 and the ending value is 1.
10:16And this says NA is introduced by coercion, which isn't good.
10:21If I really thought that there were NA values, I could just look at the data here and see.
10:27Or I could do some, whoops, is NA.
10:38This is saying that there are no values, which means that that was just a warning.
10:46Finally, I want to rerun this code.
10:55Yeah, so I want to count the column that I've just created to see how many values,
11:01how many observations fall into each bucket.
11:04So I can just count N exams.
11:09So first, yeah.
11:10So if I do this, you can see that there are 12 records with one exam.
11:19There are 17 records with two exams and so forth.
11:22So this looks to be correct.
11:25Question 8.
11:27Create a column called Social Life, which is equal to N exams divided by experience.
11:33What is the average social life by industry?
11:38Okay, so again, I want to mutate.
11:43And then say social life is equal to N exams divided by experience.
11:51And then group by industry.
11:56And then summarize.
12:00So that social life is equal to the mean social life.
12:07There you have it.
12:09Notice that programme?
12:09Thanks.
12:11Bye.
12:12Bye.
12:13Bye.
12:14Bye.
12:14Bye.
12:15Bye.
12:15Bye.
12:15Bye.