and now you will have an explanation about this command by sort when used with this command agent
we're going to use by sort agen mean to find the mean of a specific category of data and we're
going to use by sort agents um to find the total of any category of a set of data the first thing
that by sort does is it sorts by one category or you can think of it as sorting by one categorical
verum variable and then after you do that you get the mean of another variable for example we
want to know the average spent on snacks but of only the respondents who rated their enjoyment
as three out of five on the enjoyment scale we would have to sort the data by enjoyment first
and then take the mean of the variable snacks for only those respondents who rated their enjoyment
at the pool as three now this is easier to see in Excel so I open up the Excel spreadsheet and you
can see this is our data sheet except it's open in Excel here's the column of enjoyment so enjoyment
is a categorical variable it's really ordinal people rate how much enjoyment they get at the
pool on a scale from one to five so here are the results that all the respondents gave but right
now it's not sorted by level of enjoyment so if we highlight the entire table and then we click data
sort and we sort by the column of enjoyment then when we click OK notice now all of the
respondents who rated one are grouped together all the respondents who rated their enjoyment at the
pool level two or grouped together together and so on so all the respondents and all the rows are
grouped together by this variable so now if we took the mean of these three people these three
respondents and what they spent on snacks then we would have the average spent on snacks from people
who rated the enjoyment as number one same thing with these three people if we took these three
numbers and took found the mean found the average we would know the average of the average amount
spent on snacks by people who raided the pool as - on a scale of one to ten so these three these
four people hey these people rated the these these four people rated the pool as level of enjoyment
three that means that these that means that these four people if we took the average of these four
people we would get the average we would get the mean spent on snacks by people who rated their
enjoyment as three now here's how the code works you type in the command by sort then you type in
the variable that is the category that you're going to break it in to make sure you put a
colon then you type in the command ijen then type in the name of the new variable because remember
when you see that mean you're going to see a new variable an additional column show up to the right
of the data set and that equals the mean of the old variable so in our example this is exactly
what the code will be by sort enjoyment we're sorting by enjoyment and then the new variable
mean snacks is equal to the mean of snacks but it's equal to the mean of snacks broken up by
the categories of enjoyment and you will see we will type in exactly this code and then we will
look at the data and then you will understand now again you can pause the video and type in
the code slowly but I have already explained it so I typed it in carefully I'm going to push enter
now I did not get an error message so let's take a look at the data aha there is a new variable
here end notice that data is sorted by enjoyment for all the people who raided it one this is the
mean spent on snacks by the people here's what they here's what they spent on snacks and here's
the mean of what they spent on snacks for these people who rated it as one see the people who
rated it as two here's what they spent on snacks and here's the mean you see it's a different mean
from that one category here's the one category mean here's the two category mean here's the
three category mean you see it changes means when it changes categories and the entire data set is
sorted by enjoyment now let's try example two we want to know the average age of only the men and
the women respectively average age means we want the mean age we would have to sort the data by
gender and then take the mean age for both male and then female so here's the code again here's
the general explanation of the code but in this case we type in by sort command because we're
sorting by gender don't forget to put the colon then type the ijen command to make a new variable
in this case we'll call it mean age and we'll set it equal to the mean of the variable age but if
you put this after by sort gender it will take the mean age by the category gender so now I've typed
in the code while I was off-camera to make sure I don't make a typo when I push enter I did not get
an error message so that's encouraging and when I open the data aha now there's another variable
called mean age and if you notice the data is sorted by men and then by women all the males
are up here and the average age of the males in this survey is thirty two point three and
then here are all the females and if you go all the way across the average of their ages the in
the survey 25.4 so you can see that even though the data of the variables are not next to each
other you can see that the value of every single one of these first ten in mean age will be the
same because that's the age of the males and it's the same thing every single one of these fields
will values will be the same for this variable because they correspond to the category female