R in DataCamp Workspace and Building Data Products

Ramnath Vaidyanathan walks through DataCamp Workspace using R to analyze U.S. baby names. He loads data with readr, manipulates it with tidyverse functions, visualizes with ggplot2, and publishes the notebook as a shareable HTML link. The session covers building data products and answering questions on mutate vs. summarize.
Chapters
Hey everyone. First of all, thank you for attending the second workspace live Code along a colossal thank you as well to the speaker Ramnath Vaidyanathan, who together with datacamp and especially all of you made this event possible. First of all, we share the link to the template that you will be using throughout the session in case you have not opened it yet. And finally, we would like to remind you that the session will be recorded and the link to the video will be posted through Slack, email and social media platforms. Next slide please. So, a little bit about Data Camp itself. Our mission statement is to democratize data science education and make data fluency possible and accessible to millions of people and businesses around the world through our learning platforms and now our practice through workspace and events such as these. A little bit about our speaker today, Ramanath Vaidyanathan. He's the VP of Product Research at datacamp where he drives product innovation and data driven development. He has over 10 years experience doing statistical modeling, machine learning, optimization, retail analytics and interactive visualizations. So you name it, he's done it. So you can trust you're getting high quality content. So today's code along is divided into several parts. First, the speaker will solve the first question of the template whilst also showcasing some features of workspace itself. This will be followed by a Q and A and this part will be around 20 minutes. Next up, Ramnath will also go over the second question and do a Q and A for that part as well, which will also be around 20 minutes. Next there will be a small presentation about data products and that part will be around 10 minutes. And then we will have the remaining time until we reach the one hour gap for your questions about the topic. Finally, the speaker will give some remarks about the template and I will say some closing words. The whole session is scheduled for an hour and we want to remind everyone that this session is recorded and the link will be posted later on Slack, email and social media platforms. Thank you for your attention. And now into the event itself. I pass the stage to our speaker, Ramnath.
Thanks Yash. I second Yash in welcoming all of you to this code along we have a fun packed one hour session where we will be exploring a personal favorite data set of mine, the baby names data set that many of you might have already seen. But the idea is that we will be using this data set and then using workspaces to analyze and come up with some interesting insights. Without much ado, let's get to the workspace. The link to the workspace should have already been shared with you before and also in the chat. Make sure to open it and if you open it you will basically be greeted with instance of rstudio and there will be R markdown document named notebook rmd. This is essentially where we'll be doing all our coding. Before we get started, a few words about Workspace and why datacamp built Workspace in the first place. So for those of you who use Data Camp to learn, you're familiar already with the idea of how datacamp does interactive learning. But our goal is not just to help people learn. We also want people to be able to take what they've learned and apply it in a real world setting and in fact use tools that are used by real data scientists. The Workspace product was primarily designed for two reasons. One essentially as a platform to move from learn to essentially more practical and more real world platform. And the second is basically enable individuals and companies to do data science in the cloud without really having to worry about installations, discrepancies and basic setups and things like that. So Workspace essentially is our data camps answer to address this problem of people having to set up things in doing real data science. And quick summary again, Currently we support RStudio for R& Jupyter Lab for Python. So These are the two IDEs that are supported. There are several rich features available here. A couple of them I will just highlight before we move on to the code along. First is you can actually publish your work. I will show you as part of the code along. You can actually translate, run this and create HTML file and essentially share the link so your analysis can be published. Second, for those of you coming from Learn, you can actually for projects that you've completed, you will be able to open your project completed project in a workspace and that will essentially allow you to publish your work and even enrich your work with more questions, more analysis. So I would strongly recommend if you're not already checked workspace out, definitely check it out. So with that, let's dive into the meat of today's code along. So this code along, as I explained earlier, is a really rich data set of the first names of babies born in the United States. It spans a period of more than 100 years and it's a very simple data set, but it can help uncover lots of interesting stories. So every time I look at this data set, I can always find new things to do with it. It's really fun to explore. I'm going to skip the installation of this package just for now. We don't need it for this code long. Don't worry about running this cell yet. We will start with basically importing loading packages. For those of you not seen R Markdown notebooks before, the way R Markdown Notebooks work is it consists of two parts. So you can type free text, but you can also type code between these two markers. So basically the marker is a triple backtick with a parenthesis and R and a closing parenthesis and another triple backtick. If you need to insert a code chunk, basically you can do it using keyboard shortcuts. I'll explain, but the best way to do it is to go to this menu and essentially whatever code chunk language you want to insert, you can basically insert it and it's going to insert it for you. The idea is you combine code and text and and code and text together essentially form your final report. So let's run the very first code chunk here. And what this code chunk does is it just loads the Tidyverse, which is basically a universe of packages that will make data analytics easy. We are also setting some publication options here. Basically, R Markdown uses a package called Knitter. Here we are setting. I'm increasing the dots per inch for any images generated so that it looks nicer. We are also changing the dimensions of the width and height of the figure so that when you publish it, it looks nicer. These are just a couple of settings that are good to have. The way to run this code chunk. There are two ways to run any code chunk. The first, before we do that, I'm just going to maximize this screen so that you can see more of the code. The way to do it is you can go into this panel menu and you can click Zoom Source. There's also keyboard shortcut control shift 1. So if you're using it a lot, you can use that Zoom Source. And basically this is going to expand it. I'm also going to go into full screen mode so that there is maximum screen size utilization. Okay, going back to how to run the code chunk. There are two ways to run the code chunk. The first is to click on the Run button. So basically, if you click on the run button, you will see that it turns red for a brief period and then it becomes green again. And that means that the code in this cell has been wrong. Now, you don't see any outputs here because there are no outputs. But then there are outputs. You will see that there will be outputs. Let's get started. The other way to run the code chunk, if you use it very often, is essentially Control Shift Enter if you don't see me clicking on the run button, are intrigued. How is the code running is because I am basically using the keyboard shortcut. Control Shift Enter. Okay, now that I've explained the basic ideas of R Markdown, the basic ideas of workspace, let us move along and start with analysis of baby names. So the first step in any analysis is to read the data. In this case, the data actually is already provided for you in the template. It's a CSV file, but it's been gzipped, which means it's been compressed. And that really reduces the amount of size that the file occupies. Then we're going to use the readr package and the read CSV function. For those of you who are not familiar with the double colon syntax, it basically means take the read CSV function from the reader package and then run it. In this case, the first argument is the name of the file. So this is names CSV gz. I also have a second argument and I will explain to you why this argument is required after I run this code chunk. So this is the baby names data set. And you see that it has four columns. The name, the sex, male, female, in this case the number of births, and the year in which this observations record. Right now, the reason I'm adding a call types argument here, for the most part, Radar will automatically infer what are the types of the columns. But in this case, the problem is that reader, the way Radar infers it is by looking at the first hundred rows, and in this case the first hundred rows. The column with the name sex it has F all along. And so if you don't use this read CSV function will interpret this as a Boolean column because S in R stands for false. So if you don't use this, you will see that sex is imported as a Boolean variable, which is not correct. So that's why we explicitly say that. Okay, hey, reader, please treat the column sex as a character. And this is a syntax to do it. Okay, now that you read the data set, I mean, it's a deceptively simple data set. Like, that's only four columns, but you can do a lot of analysis with this. I have identified six questions that are all interesting, but we will go over two of these in this presentation. But you can also look at the other ones. You will have everything available in the notebook. The questions that we will explore today are going to be what are the most popular names? And we're also going to explore what are the most trendy names in the US and time permitting, yeah, we will see if we can get to unisex names. But I want to focus on making sure that everybody is able to get things working rather than covering more in the code log. So one other thing just to make sure you all know, the same template will also have a notebook called NoteSolutions RMD. This is basically the solution code to all these tasks. The reason we have provided it is in case you get stuck in one question and you can't figure it out, you want to move along. This allows you to look at the solutions, copy paste it, and that way you're not getting stuck. I would strongly recommend using it only if you get stuck. If you don't get stuck, just continue coding along with me. You'll learn more, you will get more out of this. Okay, so, so one of the very first questions that, that I think data set begs to be asked is like what, what, what are the trends in names? Right? What are the most popular names? So the, the question again there are various variations you can look at but for the purpose of this code along the question we will be exploring is what are the most, what are the top five most popular male and female names of this decade? So born 2011 and later, before we get get to answer this question, I just, I mean please feel free to make your guesses in the, in the chat. So if you have, if you, if you think, okay these, I think this is going to be a popular, this is a popular male name and in this decade just feel free to add it to the chat. We'll see if any of you are able to correctly guess what the names are. Okay, so let's start with the analysis. So usually in the tidyverse and in dplyr, typically you use the pipe, which means that you'll start with the baby names data set and we will carry out a sequence of operations that will give us what we need. Right, And I'll go with the simplest possible operation here, which is head. Head is essentially taking the top, displaying the top six rows of this data set. And so if you do baby names head it is going to list the top six. Name the top six rows. Okay. And the reason this style became, has become very popular in the R world is that that it leads to really clean code where you can really understand the sequence of transformations used to arrive at a solution. Let us start with if you're familiar with tidyverse, this is going to be pretty familiar. If not, don't worry. My goal is also to take you through some elements of Tidyverse So the first thing we need to do here is we need to only look at names of babies born on or after 2011. To do that, we use the filter function. And the filter function, basically the argument it accepts, it just accepts a condition. So you can say that I want year greater than 2010. Let me execute this code. And you can see that now you'll see that this only has baby names starting 2011. I haven't ordered it. So that's why 2015 is, like all the way on the top. Okay, now to get the most popular male names and female names, we need to group by. And so I didn't mention this. So if you. If you're wondering about what this symbol here means, this is basically the pipe. It's a magnetar pipe. And what it basically says is that, okay, take this argument and take this value and then feed it to the next function, right? So this what it does and the shortcut in RStudio to kind of generate this is control shift M. So, I mean, you don't want to be typing this all the time. So control shift M will automatically give you this. So now that we have filtered, the next step is to group. Because remember, we want the top names by for males and females, right? So we want to group by two variables. The first, we want to group by group by name. Actually, let's group by sex first. Group by sex and by name. And what we want to do after we group by sex and name is like we want to get the total number of births, right? So we want it aggregated across all years. So the way to do that is to use the sum. To use the summarize function. So you can say summarize. And in this case, let's say you can say births equals sum of births. So what this is saying is. Okay, this will recap. Take the baby names data set, filter it, keeping only those rows where the year is greater than 2010, group by sex and name, and then summarize by. Summarize the births column by adding up all the births. Okay, so let's run this. Let's see what we get. So as you can see, you have sex, name and births, right so far. Good. Now, one of the things that you need to note is that. And something that's a little subtle in dplyr is that this data set, this data set actually is a grouped data frame, okay? And you can see that it says groups, sex. So this data is still. I mean, it shows up as a single table, but it is still grouped by sex. Which basically means any operation you run after this will be run separately on each of the two groups. Right? And that's actually good for us. And because right now, think about it, we want the top five names, right? For each sex. And so you want to run the operation on each sex. And that's why I picked this order. If I pick the opposite order, then the problem is your data frame is going to be grouped by name, which is not good because we want the top five names by sex the order. That's why I ordered it this way. It's a convenience provided by dplyr, but often can trip people up. So that's why I always pay attention to what the output says. And it's clear here, that's actually grouped by sex. Okay, let me reduce the size here so that. So now how do I get the top five rows? There are two strategies here. The first strategy is that you can arrange the births column in descending order of births. You can arrange this data frame in descending order of births. And then you can take. You can use head to pick the top five. But a more efficient way, or rather a simpler way, a cleaner way in dplyr is to use this function slicemax. And what slicemax does is it basically slice, essentially dplyr verb for selecting a bunch of rows. And here slicemax says that you want to select by the maximum value of a particular column. What column is that? Well, I want to pick it from the birth column. And how many do I want to pick? I want to pick five. Okay, so let's run this. And now you can see that you have the top five names. Okay, let me give this, save this to a data frame and just call it popular names. Advantage then is that we can basically do a lot of plotting and other interesting things with it. So let's run it again. And there you go. So you have the top five names. I didn't get to see the. See the chat, but yeah, in case any of you guessed the names correctly. So Emma is the most popular female name of the decade, whereas Noah is the most popular male name of the decade. And yeah, so which probably means there is a high likelihood that there is someone named Emma Noah on this. In this code alarm. So now that you've gotten the names, the next step is to really plot, is to plot. Plot it. So the plot we're going to basically choose here, we're going to choose a horizontal bar plot. Okay. And the reason we're choosing a horizontal bar plot is usually when you have text as one of the Columns, it's better to do it on the X axis because the Y axis, it'll occupy more space on the X axis. So you want to really pull it on the Y axis. So how do we do that? So to do that, we're going to use ggplot, which is really good package R package, to do visualizations. And ggplot also supports the piping syntax with a little change as you go to the next steps, and I will highlight the change as we do it. We're going to take popular names. And the way ggplot works is that it starts with a call to ggplot. One of the first things you need to do is to tell ggplot what are the columns and how are you mapping those columns and explain what it means in this case. Remember, we want a horizontal bar plot. So we want the X variable to actually be the birds, right? Because the birds are going to be along the X axis and the Y is going to be name. So let's just do that. And the way you do it, you call a function called AES, which means aesthetic. And it's essentially a nice wrapper to sort of specify the. The inputs to the plot. So we're going to say X equals births, comma, Y equals name. And now we want to tell ggplot what kind of a plot I want to create. And here is where the piping syntax changes a little bit. You add a plus. And there are historical reasons for this. Ggplot was built before the tidyverse, although ggplot is a part of the tidyverse now. So at that point in time, the operator that ggplot chose was the plus operator. So that's why you see the pipe and plus. But pretty much the way to read this code is start with the popular names data, send it to ggplot, and add birds and name as the X and Y columns. And finally, to do the actual plotting, we will use the GEOM call function. So what this is saying is that I want a column plot. So let's run this piece of code. Let's see what we get. Okay, so we definitely got something that's interesting. So we got the birds, we got the name, we got a horizontal plot, but let's not stop here. So one of the things that data visualizations is that it's really easy to get to a base visualization very fast. But to get to a visualization that is impactful and something that communicates the message, the story, it's important to really work through other details. And I will on purpose I didn't really introduce that in the beginning because I wanted to show you how you can think along and clean up this plot in steps. Okay. So one of the very first cleanups I want to do is this shows the male and female names in one plot. Obviously, that mixes things up. I want to show them in two different panels. How do you do that? Well, ggplot has a really convenient function called facet wrap. And facet basically means panels. In this case, we are going to use only one facet, and that is sex. I'm going to say sex Tilde dot. The dot stands for I don't need any other facets on the other dimension. And let's just run this and see what we get. We got two panels. We got two panels. But I'm not too happy about this yet. Reason I'm not too happy about this yet is because, you see, it's essentially giving you. The Y axis is the same. So that gives rise to all these kind of gaps, which really doesn't make for good viewing. So how do we kind of collapse these gaps? So the reason why ggplot did it this way is because it shows the same scale for both the facets. That's the default. So what we need to tell ggplot the facet wrap function is that, hey, I want three scales along the Y axis, which basically means keep the X axis consistent. So that, I mean, you could compare the bar length means the same in both plots, but the Y axis is just names. So I don't want you to. I want you to enable both the facets to have separate scales. Okay, so let's run this now. Okay, now we are getting, getting somewhere, right? Definitely Much better where you have two panels, you have the names, but still, something is kind of like seriously missing in this plot, right? And what's missing in this plot is that it's nice to kind of order these columns, order these bars by the. In decreasing order of births, right? Because, I mean, that makes the story much clearer. It's very hard to see which is the most popular name here. You have to spend some time looking at it to decide that Emma is the most popular female name. How do you do that? This is actually something that you do in the previous layer. It might seem a little convoluted, but once I explained to you, you will understand how it works. What we're going to do is we are going to modify the name column. The way we're going to modify the name column is we're going to use a package in the tidyverse called for cats. The cat here does not stand for the animal cat. It stands for categorical variables. In case you're wondering why is the package called for cats for cats. And we're going to use a function called fctreorder. And what the FCT reorder function does is, is it takes a particular column and it reorders the levels of that column based on another variable. In this case, I want to reorder it using birds. Let me run this code and then you will see what happens and then I will explain what the code does. Let's run this. And you can see that now Emma is right on top, same way Noah is also right on top. So it is basically. It basically respected the order. That's what this particular row of code does. It said, please take names, but reorder them in order of births because you reordered the names. Ggplot picked it up and was able to display it the clean way. There are a couple more things you could do aesthetically. I will do one more, I'll do two more, actually, and then I'll leave you with more things that you can do so that we can go to the next question, of course, the Q and A and then the next question. So a couple of things you can do. One is like the scale, right? I mean, these numbers are big, so you don't the zeros really. I mean, it's too much information density. So what you're going to do is we're going to say scales, scale, x continuous. So we're going to tweak the X scale and the way we're going to tweak it. Actually, let me go back to the previous one so I can get autocomplete.
Here.
We're going to say labels, scales, unit format. We're going to unit format it. And then what we're going to say here is scale equals. So in this case these are all like, I think tens of thousands. So we can basically say scale equals 1, E minus 3 and we can say unit equals K. I'll explain it after I run the code. Now you see that the X axis is nicely formatted in thousands, so that it works a lot better. Couple more tweaks to make here. One, definitely the most important one is labels. If there is one thing you can take away about visualizations from this code along is that never publish a visualization, never share a visualization without a title and access labels. Okay? So in this case, the title, basically, I mean, you could use different titles. I'm Going to say most popular, most popular names of the decade in the usa. And then I'm going to set the X axis label as number of births and the Y axis label. The Y axis label. Actually you could set it to null because it's not very informative. Anyone who looks at it, I mean, will know that it is a name. So again, people have different preferences. I generally prefer not to. If it's an obvious label, just skip it. So you reduce information density. So there you go. So now you have the most popular names of the decade in the US Just one final thing I will add before we kind of take questions. There are two other really cool parameters you can add here, which is subtitle. And in this case, subtitle is something that appears below the title. So, for example, Emma and Noah are the most, the most popular are the most popular names of the decade. And I can also add a caption. And a caption is usually something that appears at the bottom. And here I'm going to say this is where does data come from? Social Security. Data set is from Social Security of the US So, so now you go. And now you can see that, I mean, there is source ssa. So at this point we are going to, we're going to take, we're going to take a short break. I have one more, I have one more question here, which I will take after the, after the break because I also want to keep track of time so that we are covered. But if you have any questions till now, please. Yeah, feel free to put it in the chat, take a couple of minutes and I will address those questions. Awesome. So let me answer the questions that already already been asked. So one of the first questions is like if you already stated Tidyverse, what is the need to include readout? Is it not already a part of the Tidyverse library? Excellent question. The question is that, okay, if I have library Tidyverse, why do I need readout? The answer is yes, I don't need a radar here. It's just a matter of habit for me. Like whenever I call a function, a lot of times I just namespace the package name in front of it. So I would blame it on that habit. And to a large part, sometimes it helps, especially when certain functions have, I would say, conflicting names, but. Absolutely right. You don't need this here. Even without read our read, CSV will work because Tidyverse will load the radar package. Next question is, where did popular names come from? Is it the result of assigning the code from step one to the table name? Absolutely. And I should have. I probably glossed over it a little bit in detail. The detail in it. So remember when we started, I didn't really have popular names here. I basically just ran this code and the reason I didn't add the variable initially is that I wanted to quickly look at the outputs. But what I did at the end was once I was happy with the output, I said, okay, now I need to save it to a variable. And basically I did popular names. So I assigned that table to this. There is a neat trick that I would recommend using with high caution. I mean, definitely don't use it in code you share with other people, but it's okay to use it when you're doing it quickly. This is an operator that some of you might not be familiar with. So this is the assignment operator in the opposite direction. And advantage of that is you can basically put it at the end of the pipe and it leads to the same result. Okay, but don't use this unless you're in a hurry. Definitely not in code that you're sharing with other people. I will move it back so that I don't do it this way. Let's take a couple more questions before we move on to the next thing. Is there any way I can order the Y axis by name rather than birth? Absolutely, you can do it by name. Basically the way way to do it by name is that you need to sort, you need to sort the names in, I mean alphabetical order, either decreasing or increasing. So rather than do mutate name equals for cats, you will be doing it that way. Okay. Although in this particular case I would recommend that the better thing to do here you will see in a later case you will see where the alphabetic sort actually makes sense for names. I would recommend like keeping the sort to this. Let's couple of questions. Is the for cats package part of the Tidyverse? So the forecast package is, is a part of the, is a part of the Tidyverse. Although, I mean I am, I, I don't remember if, if it will. If the Forcats package will. If Tidyvers will load the forcats package. Okay. And I can, I can, I can check it, but I don't. That's why I always kind of are on the side of caution and basically add, add this, add this directly. Okay, so a few more questions. How would you add labels to each observation, like the count of each birth for each name? Fantastic question, Hannah. And that's, that's really thinking about how to, like, how to really make your plots more informative. So Hang on, I'm just running into a small, small issue here that I will. Okay, the package installation kicked off. I should have, I should have commented it out, but I will show that particular, that particular part. So the way to do it. Do it is. Easiest way to do it is to, is to mutate the name and basically say paste. And in this case I'm using paste just as a quick win here. Paste name and then. Or actually let's use the glue function. Let's just do tidy. So glue function. And the glue function basically allows you to provide a template. And in this case the template is name and then inside parentheses. I'm going to add, I'm going to add births. But yeah, I'm going to add births here. So it's basically adding some extra information. So if you now run this piece of code, you will get the births added. And I'm missing a parenthesis here, of course. And yeah, so let me run this again to get the plot. Just give me one second. Okay, yeah, let's wait, let's wait while this runs. I'll go through the other questions. I probably have time to take a couple, a couple more questions, then we can go to the next one. Can we alter the titles of the facet panels from M to F, M and F to male and female? Absolutely. Ggplot is very flexible. It allows you to do all of that. To be frank, I don't remember top of my head how to actually rename the panels. But if you just search for ggplot and Legend, you will find tons of examples of how to do it. Ggplot is a vast universe of things. So yeah, sometimes you don't exactly remember every piece in there. Okay, so I can't show you right now because I don't remember top of my head, how to do that. Ok, so let's just get to the last one. How do I know what arguments from function ggplot argument to scale X continuous. Good question. There are two ways to actually do it. One is by if you're in the function and you press tab. Sorry, let me just take this out first so that when you start typing, RStudio will display the options that you have for this particular function. And the other way to do it is actually you can do this in your R console. You can basically say question mark, scale X continuous. Actually, I think I need to load tidyverse explicitly here. Just give me a second because there's a problem with my session, tidyverse. And yeah, let's do Scale X continuous. So if you do Question mark. You will see all the options that you have. Okay, so for example, and the documentation will pretty thorough documentation. So you can see what are the various options available for you here. Okay, awesome. So I am going to go to the next part and we're going to quickly explore that part because I also want to get to the, to the last part of the presentation. Let me do a couple of things to set the session back. I am going to comment this out because we don't need this package. And I am going to just restart my session because there was an error in my code that led to this. And then I am just going to run all the code chunks still here. This is a convenient thing in R Markdown where if you want to just run all the code chunks still a particular code chunk, you can just click this and it's going to seriously execute everything till that code chunk. And then of course you can execute the code chunk that you're looking to execute and you'll get. Okay, so one of the things I want to show you at this point before we go to the next question is this is basically the publication feature. So we can actually publish this. And the way to publish it again, I'm not going to do it right now, but the way to publish it is you basically click on the share button and then it'll bring up this screen for you. And on the screen if you. I mean right now it's showing update because I've already published a version of this. So in case you're not publishing, it's going to show you publish. And once you click on it, you will get a link and let me just click on this link so you can basically see what it looks like. In this case, this is the solution that I published and it's basically the entire R markdown executed with all the outputs basically in the document. Now a couple of other things that we have added so you can. In this case I've shared it with everybody, but you can also share it with specific people. Strongly recommend exploring these features out. And you can also show it in your profile page which I will describe at the end as a means to create a portfolio of data science projects. Okay, so at this point I'm going to actually skip this question. Leave it as something you can do yourself. Plot the trend in number of babies in a specific name. So I leave it as an exercise. Solutions are there so you can look it up. So let's move to the next one. Next question. Next question to address is like trendy names, right? So let's say we want to understand, like, are there names that are, like, very popular in one or two years, but, like, just popularity dies down, right? Or it's never been popular in any of the other years. So the one way to do it is there are many ways to define trendiness. Like, one of the easiest ways to define trendiness would be by taking the maximum number of births in any given year for a name and dividing it by the total number of births for that particular name. Okay? That will basically give you a way to kind of measure these spikes. And in this case, let's just do baby names. So to get trendiness, I always like to start with the raw data set so I can take a look at it and I can look at the transformations made. So in this case, I have name sex births here. So one of the first things I want to do is I want to compute the trendiness of a name, which means that for every name, right? And I'm also going to add sex here just because it makes it easier. And what I'm going to do is I'm going to. I'm going to mutate, okay? Mutate means add a new column. And the new column I'm going to add is basically a measure called trendiness. And what I want trendiness to, how I want tenness to be computed is births divided by the, sorry, max births divided by sum of births, okay? So this will essentially compute, add an extra column to this data set which will contain, like, the. The trendiness factor, right? So the one thing we want to be careful is that we also want to compute the total number of births. And the reason is that it does not make sense for the name to be called trendy if, like, only two babies were born with that name, but it just happened to be in one year, right? So we want to look at popular trendy names. So to do that, we're going to also calculate the total births, which can be done with sum of births. And finally, I want to filter by the total births. Like, I only want to look at names that have at least 5,000 people born with that name. And finally, I want to sort in decreasing order, right? Now, one thing to remember, we group by name and sex here. What I'm just going to do is I am going to reverse the order because if you remember what I explained last time, we still want it to be grouped by sex. When I do the next operation, the next operation is going to be ordering. And in this case, arrange is the dplyr verb to order. And the Arrange. I want to arrange them in descending order of trendiness. So arrange descending trendiness. And finally what we want to do is we want to keep the top five so we can do head five. And remember, this is basically what we did before with the slicemax function. I just wanted to show you another way of doing it which is arrange and then slice of course is a much cleaner solution. So let's get this. Let's wait for this coaching to finish. So one of the. So this is not really giving us what we need. And let's see what is happening here. Okay, so this is the reason why you're getting this. Okay, so if you remember, last time we used group by and summarize. And when we use group by and summarize, once we finish the summarization, we got things correct. So I made a mistake here where the mute. I should not have used mutate here because I'm adding an extra column. But I want only one row per sex and name. So mutate was actually the wrong function to use here because mutate essentially added like, I mean you can see how it added. For every row it added the same value. Right. So I made that mistake here. So I should actually do summarize. And so that's one mistake I made. And second, because I do summarize, I have to pull both the summarize within the same. Within the same summarize call. So just to recap, basically we're going to summarize and we're going to compute two numbers. We're going to compute the trendiness and we're going to compute the total number of births. Okay. And now you will see that like what we have actually will work or should work. I'll wait for the, wait for the names to show up. Well, it did seem to work and work here. Although I mean it basically it basically listed only, only the top five. Only the top five names here. So I'm going to make it, make it 10 here so that we have like we have a total of 10 names. And it also does sort by sex, which is, which is. Which is. Okay, I am just going to read on this code chunk so that we get the final output. Awesome. So now we see some interesting names here like Ashanti, Shaquille. I think a lot of popular celebrities, like, I think a lot of their names might show up as trendy names where they become very popular. People start naming them, naming their kids after them and then popularity dies down. Name popularity also dies down. So you can explore this in more detail. So let's kind of stop here to answer more questions as we're moving along towards the end of the presentation. And yeah, let's see if there are any, any, any other questions that people have. Okay, so I, okay, so I don't see, I don't see many new questions here. Yeah, we could, we could do a lot more. As you see, like the solution notebook has like a ton of goodness, but unfortunately because of time, we'll kind of have to move on. Before I move away from the, from the presentation itself to the next part, which is like just talk about data products, I think is a useful topic for anybody doing data science, I just want to mention that this notebook is shared with you and at the end of the notebook you will see a link to another data set with French baby names. So you can either analyze the same data set that I gave you and explore insights, or you could basically start the French baby names data set and uncover new insights. Okay. And there is also an Australian baby names data set that we don't have yet, but we will provide at some point. So yeah, I hope you enjoyed this part of the code along. I'm just going to switch quickly to the presentation just to kind of round up on the data products bit. I'm seeing a couple of questions that I will address right after I finish the data products. 1. Okay, so what exactly is a data product and why are we talking about it in the context of this presentation? If you really think about the job of data scientists, the job of data scientists ultimately is to provide data insights. I just provided this slide to give you a full picture of how data works in any organization. It always starts with the raw data and there is steps involved in that housing theme, steps involved in processing them, accessing them. But as data scientists, finally we deliver data insights and that's primarily done through data products. Right. And the example I could give you, for example, the baby names analysis. Right. Let's say it's something that we need to share. We can share it as a data product, either as an article, or you could create a dashboard and share it as a dashboard. The advantages of dashboards are that it's more interactive. Your users can ask more questions by clicking through various bits and pieces. So it's very important to keep data products in mind as you go through your data science journey because it's the ultimate step in. It's called the last mile. And the more successfully you can do data products, the more successful you will be as a data scientist. I'll just give you two examples. One Here at Data Camp, for example, this is an article I wrote on our curriculum. And as you can see, there's a workspace right now. These are essentially articles that can be shared with people across the organization and, and essentially you can look at it as a data product. A more advanced example of a data product is something, for example, we share with all our instructors. Every instructor has a dashboard that the data science team built where they can see all the metrics as far as their courses are concerned. They can look at what are the ratings of the course, how is it varied over time, how are the completion rates, how many feedback messages people have given. And this is actually something that we built with R and Shiny, which is a dashboarding framework that makes it really easy to build these sort of things. So if you're interested in data products, we have a number of courses to help you build data products. But the key message I want you to take along is that while you pursue your data science journey, please remember that what matters a lot is the final mile. All these bits are really cool. The first data pipelining and processing and access. Access. But ultimately, as data scientists, you need to think about yourself as creating data products. And taking that mindset will really help in your data science career. Okay, so with that, I'm going to take the last set of questions and then give final remarks, then transfer it over to Yash who will give his final remarks, and that's basically where we'll end. So I'm going to return back to workspaces basically to answer these questions. So one of the first questions, how do we save our work in workspaces? Good question. So the saving is usually automatic. So every frequent time intervals the notebook is saved. To be on the safe side, I would always recommend explicitly saving it, but for the most part you should not have any issues. There is an autosave that will happen from time to time. Another question is, can you quickly repeat the difference between mutate and summarize? Great question. It's one of the most common sources of confusion for people new to DPlayer or Tidyverse. So the difference between mutate and summarize is that actually, let me just do it here so that you can see it. Let's actually do it for the first part. So when I do mutate, right, let's actually do a couple of things. Let me actually count the number of rows in the data set. This will help you understand what's the difference. You can see that approximately 328,542 rows. Okay, now If I mutate, if I compute mutate births equals sum of births and then I count, I should get the same number of rows. Okay, because mutate really doesn't. I'm getting this error. Mutate really doesn't. Condense, condense the, condense the number of rows and let me. Actually I need to ungroup here to get the count. So this is another subtlety of using the group by here, but let's just do that here. So you can see that group by doesn't collapse the number of rows. It just adds an extra column. And in this case you will see that the. The extra column it added. Actually, was I running into this error because of my connection? Most likely you see that for the same name it will add the same number. It'll basically add up births for each name and just display the same for every year. Which is not what we want, right? What we want is one row for each combination of name and sex. And that's basically what summarize does. And the way to see it quickly is again, I'm going to do the ungroup and count because that's what will give you the right number here. So if you do an ungroup and count, you will see that it's 59,300 rows, which is basically like all combinations of names and sex. Okay? It takes out the year out of the equation. So if you want just add a new column, keep the number of rows intact, use mutate. If you want to add a new column, but it is an aggregation of some grouping variables, then use summarize. Okay, let's. One more question. Why does summarize remove here in the code chunk? How would the code be changed? So here is in the DF again, very good question. So here is the. The reason you don't keep year in. In the summary is that I want to look at the most popular name across all the years, right? So that is why I'm computing. I'm computing birds across all years. So keeping. Keeping years in this, as, as a column in this data set does not. Is not going to be useful. Okay? Now if you do want to keep it, well, mutate will let you keep it. Because what mutate does it add this, but it will just keep all the existing columns. But in this analysis it's not going to be useful for you. So that's why we use summarize and really don't bother about the year not being there. Okay? So I think, I think those are all the questions that I'm seeing here before I hand it over to Yash. Just a couple of remarks. So definitely check this out. We will be sharing all the links, the solution notebook. You can publish your present, you can publish your work and you can get a really nice profile. Let me just share my profile here. You can essentially publish notebooks and you can get a nice profile of all your workspace publications as something that is cool to share as a portfolio of data science work that you have done. Okay, check out the French Baby Names data set. And we will actually be announcing a competition in a week or two where you will get the chance to play with the data set and it'll be a competition. So keep stay tuned for that announcement and hope you explore workspaces. And please feel, please feel free to give us your feedback. Our goal is to improve the product and make it a really good experience for you. So I'll hand it over to Yash to make the final remarks before we end this code log.
Okay everyone. So with that we'll be moving towards the end of the session. Thank you very much Ramnath for the very detailed and informative presentation and a huge thank you to all of you, the attendees, for your participation. Now, we will be organizing more of these presentations in the future, so please stay tuned and let us know if you have any feedback for these sessions. We will post a survey link in the chat and it will really make a difference for improving the future events that we hold. And I promise it will take less than one minute of your time. And if you haven't already, please feel free to join our Data Global Slack community where we will keep you updated for new workspace features and new events like these that are happening. So this rounds up the second Workspace Live code along session and there is nothing left from me other than to wish you a fine rest of the day and evening wherever you're tuning in from.
So all the best, thanks for joining us. Have a wonderful day.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







