Descriptive Statistics in R
Transcript
dear friends i'd like to welcome you to another episode of data science live my name is mr campanell and i will be doing a little bit of live coding with the r programming language enjoying a little bit of mukbang today we have italian american pasta but it's got a south american twist to it and i'll explain that a little bit later and hopefully provide a little bit of companionship for you here on episode number 12 of data science live so today we are going to be doing summary functions
sometimes they're called descriptive statistics and we are going to be looking at calculating means finding the maximum values and counting the number of records and the one thing about summary statistics or descriptive descriptive statistics is they are probably the most commonly used statistics in all kinds of organizations whether that's in education whether that is in government whether that is in private industry people have a good understanding of descriptive statistics
things like averages things like counts the number of total records and that type of thing so we're going to be doing that today the one thing that i do kind of want to point out that we're doing it in r these operations are generally performed on columns so in our data frames we are going to be selecting out columns and then we're going to be doing some summary statistics on those so before we get started i've been doing this in the last few episodes is giving shout outs to my new
subscribers and new followers most of these most of these are on twitch i am getting new subscribers on youtube but they are remaining anonymous so uh shout outs to all my anonymous followers and subscribers on youtube but for twitch i'd like to give a shout out to overlord zero nine nine uh mines j 1291 uh sedes abada and helfinho so thank you very much uh for subscribing to data science live on twitch i really appreciate it and we're going to be using a familiar data set
it is the oecd fertility data set today and i'm going to copy and paste the link to the data set if you want to follow along i'm going to put this into the chat so there we go that's on data science live it's an excel uh uh doc or salt excel file uh that you can download and uh use it for other exercises today so before i get started um i want to loosen my tie and unbutton this collar it's getting a little tight now because you stay home i don't exercise as much
maybe eat a little bit too much and well c'est la vie you know next getting a little fatter but we're going to be using our typical libraries today so i'm going to start with i'll put this code in here and get the prompt in here pr library dplyr yes and we're going to be review using readxl today
okay and i'm just going to load in my libraries hit the run button up here okay we've got our libraries in here so um reading in the data if you go back to some of the episodes where we use the oecd data it was kind of in a it wasn't in a tidy format like we'd like to say in the our community so i want to get it into a tidy format and there's a number of things that i have to do so i'll kind of review these
i like to put my comments in about what i'm doing we're going to call this o e c d and read read x read xls because it's an xls file and then i'm going to copy and paste that url that i put in the chat window and put it in here and there's a couple parameters that i want to put in here so we're going to skip equals three we're gonna three the first three records because there's headers in this file excel reports tend to sometimes have to have headers in them and we want to get rid of those
and then i'm going to say n max equals 42 because there are footers in here as well and this will this will just pull in all the data and now um it brings it into an uh unusual format where it just puts dots dots and the numbers for the column names so i just want to rename the column here so
name uh country country equals two and we're just renaming using the column number it's in the second column and then the last thing that i want to do is i just want to select um just the the values the columns that had the decade so that's 1970 1980 1990 2000 and 2010. so i'm gonna do select and we're gonna do country and then we're gonna use the ends with select helper and i'm just gonna put 0 in here and that's going to give us all the year so let's read this data in
oh i had an error what's my error i'm going to have to move this window up here unexpected slash slash i got an unexpected solution okay yeah um i have this it can't read it in from a url for excel so i'm going to have to use i have it on my downloads directory which if you copied and pasted this link you probably should have it in your downloads directory and that's yeah i think that's what i have to do and i have to put some quotes around here that's the thing what i like about
the live coding is you get to you get to see all my mistakes that i make and that's generally with all kinds of programming so okay i got the data in i got my 42 observations and my six variables which is going to be your country and then your five decades let's just take a look at that i'm going to pull this one down here a little bit more and yeah we see our country 1970 80 90 2000 2010. great that's our data and if we just kind of pull this down it's all numeric data which
is what we want to use for our summary functions today so our data types look good as well and i'm going to go back to coding here so i think the first thing that i want to do is i want to get the cut find the maximum value for the year 2000 so we're going to get the highest fertility rate for the year 2000 and that's that is my attempt right here
fertility rate in 2000 okay and we're going to say i'll call this max 2000 max 2 000 and that is oecd i'm going to use my pipe operator and we're going to use our summarize function here issue summarize and because um we're going to call this um max rate
and we're going to take the max of and these columns have numbers in them for the years and we have to put back backslash hash tags on those i don't know what those occur but anyway let's run this we're going to get this value so max 2000 one observation one variable okay so that maximum fertility rate was for the year 2000 it was 3.12 for that country but that that really doesn't tell me too much i don't know what country this is so i'm going to try something a little bit different
and i'm going to do a filter on this but using the max function i'll just i'll just call this max2000 all acts 2000 all and we're going to try this again oecd pipe operator and i'm going to put a filter on here so filter filter and we're going to filter on 2000 that's our column that we're going to filter equals equals and m8 max x okay so let's give this a try
and i've got i've got these six variables and i've got the value so let's just pull this up just to take a look at it so the 3.122 here was the country was india and we have all the records here for that so this is kind of an interesting thing to where you can actually use these summarizes in filter statements as well so this max is used in the filter statement and one of the things that um i like about doing data science live is i learn new things too so this is something that
you know i came up with this this problem where i only saw one value it didn't really tell me that much how was i going to figure it out so when i practice for these episodes i use this and it actually works so you're learning i'm learning this is great so the next thing that we want to do is calculate the average fertility rate in 2000 so i'm going to do this again
put my comment and a g 2 000 and this is give this a try o e c d with our pipe operator and kind of the same thing issue summarize uh average close rate equals the mean of 2 000.
i'm going to run this and again uh same thing the average rate was 1.76 and because we're taking the mean of the average of all the values in this column you know you're not you're let's say you're gonna you can filter on it but you're not gonna filter with an equals because you're to get nothing because it's likely that this average rate doesn't equal anything that we have in the data so one of the things we can do is kind of look at what countries were above or below this average rate and i think i'm going to just try to take a look at countries below the average fertility rate so we'll do this below
okay so uh below average and try this again oecd pipe operator and um i'm going to filter and like i did before we're going to do 2000 is less than and equal to a mean of two thousand right two thousand and we don't even need the equals right what i was talking about we just need less than okay and i think i might want to order it too so arrange 2000 right
okay so let's just take a look at this run this okay so below average we get 26 countries out of the 42 that were below the average fertility rate for 2000. so we're going to take this and then i ordered it so wow czech republic 2000 1.8 i mean when people stop having children there must have been something going on in 2000 because if you look at 10 years before in 1989 it's it's you know 1.89 and then it came back up again in 2000 um in 2010 so there might have been some some event
um that really kind of made made the citizens of the czech republic just stop copulating i guess that's or doing you know no children no children maybe an adherence to the better adherence to u to birth control so czech republic russia also had a really low fertility rate as well and if we go down here australia was still below but we have all these countries here a lot of this is in is in europe and as you can see the 1.48 is still below our uh below our average so you can see that
for europe and uh the oecd average is still a little bit low as well so that's that something uh something i think is is interesting well i just i just like this data set i think when you when you look at this it gives you long-term uh demographic trends and it's one of the real metrics that can help you do any type of like long-term policy outlooks and forecasting this is a really good metric to use so the last thing i'm going to do is i'm going to count the records for this and so we're going to count the number of records in the in the date in the data set or in the column we know we have 42 but you know i just want to show you um a summarize function on how to do that
okay i'm just going to move this down so we're gonna go oecd and we're going to select i'm going to just select 2 000 again oops 2000 put my pipe operator again here and then we're going to summarize by just counting the records so
you call it it's called frequency count here equals n and this n function is is what you use to uh to do your total counts pull that up and take a look at this and 1-1 42 yes i got that one right i was able to guess that one correct so time for some mukbang here that's going on i'm gonna just kind of push this down here all the way and move this up because at this point at this point of the episode it's time for you to go practice this kind of code that i put out here and give it a try
give give it a feel i'm going to just take some time grab something to eat while you do your practicing so today is it's a pasta day all right this is a kind of little medium-wide noodles here um it's a red sauce i have some some spicy italian sausage in it i say it's an italian-american dish uh because yeah that's what i think it is but there's really a variation on this one because it's really italian south american because if you'll notice it's a little kind of
on the orange color not so much on the red color that you would have from a a straight tomato sauce base and the reason is in in south america in places like brazil and argentina with their pasta they like to put a little bit of kind of white heavy cream sauce in there and that's what i did with this one i kind of like it as well so italian south american pasta and i'll just get started here
[Music] okay this
[Music]
i had to take a little break there i apologize for that you get to see an empty chair that's the first time we've had that on our data science life so so i i hope you had an opportunity to practice a little bit on these summarize functions these are going to these these are going to be kind of your kind of your main work once you get started in on a data science career these are the type of requests that you're going to get from a lot of clients within your within your enterprise
so i want you to kind of you know give it a try practice it learn how learn how to do it remember it's it's based on the data within your columns you right now in the tidy verse you can't summarize across rows although um the um the the team is is working on that so you have to summarize across uh uh pretty much down the columns for uh for this so i hope you learned a little bit uh here i will post the blog on datasciencelive.com it has all the code and i write a little bit
descriptions about what i do uh there's also the the video on there and i also take a nice little picture of the mukbang as well so copy and paste keep practicing keep learning i'm still learning as well as i as i pointed out with using the the summarize functions in the filter and i've been doing this for quite a long time so always have to keep learning so please subscribe to my channel and turn on notifications for when i go live i appreciate your companionship with me here today and until the next cast goodbye for now dear friends

Ask anything
We've read this whole study set, so questions about it get the best answers.