I have been doing google's coursera course on data analytics and am now at the…
- User
I have been doing google's coursera course on data analytics and am now at the penultimate section, but I'm having a somewhat hard time trying to learn what each R function does and how the syntax works, could you please make me a cheat sheet with what will be helpful to me, please tailor it around the course I'm doing.
Assistant
Of course! Congratulations on reaching the penultimate section of the Google Data Analytics Professional Certificate. That's a huge achievement!
It's completely normal to feel a bit overwhelmed by R syntax at this stage. The key is to connect the functions to the steps of the data analysis process you've been learning: Prepare, Process, Analyze, and Share.
This cheat sheet is tailored specifically for the Google course, focusing heavily on the
tidyversecollection of packages, which is the heart of the R curriculum in the certificate.
R for Google Data Analytics: The Tidyverse Cheat Sheet
Core Concept: The Pipe Operator
%>%This is the most important operator in the
tidyverse. It takes the output of the function on its left and "pipes" it in as the first argument of the function on its right.- What it does: Lets you chain commands together in a readable, logical sequence.
- Read it as: "AND THEN"
- Example:
# Instead of this nested mess: arrange(filter(my_data, year == 2023), desc(sales)) # You write this clean pipeline: my_data %>% filter(year == 2023) %>% # First, take my_data AND THEN filter it arrange(desc(sales)) # AND THEN arrange the result
1. Getting Started & Preparing Data
These are your first steps: loading packages and getting your data into R.
Function / Syntax What It Does Example install.packages("package_name")Installs a new package (you only need to do this once). install.packages("tidyverse")library(package_name)Loads a package into your current R session (do this at the start of every script). library(tidyverse)read_csv("path/to/your/file.csv")Reads a CSV file into a data frame (a "tibble" in tidyverse). daily_activity <- read_csv("dailyActivity_merged.csv")head(my_data)Shows the first 6 rows of your data frame. head(daily_activity)glimpse(my_data)A detailed summary of every column: name, data type, and first few values. glimpse(daily_activity)colnames(my_data)See all the column names. colnames(daily_activity)
2. Processing & Cleaning Data (The
dplyrVerbs)This is the workhorse section for data manipulation. All these functions take a data frame as the first argument, making them perfect for the pipe
%>%.Function What It Does Example (using my_data %>% ...)select()Keeps columns you specify by name. select(Id, ActivityDate, TotalSteps)filter()Keeps rows that meet a logical condition. filter(TotalSteps > 10000, SedentaryMinutes < 400)mutate()Creates new columns or changes existing ones. mutate(ActiveHours = (VeryActiveMinutes + FairlyActiveMinutes) / 60)arrange()Sorts rows based on one or more columns. arrange(ActivityDate)orarrange(desc(TotalSteps))rename()Renames columns. new_name = old_namerename(date = ActivityDate, steps = TotalSteps)distinct()Removes duplicate rows. Can specify columns. distinct(Id)to see unique user IDs.Tidying Data with
tidyrFunction What It Does Example separate()Splits one column into multiple columns. separate(ActivityDate, into=c("Month", "Day", "Year"), sep="/")unite()Combines multiple columns into one. unite(Date, Month, Day, Year, sep="-")drop_na()Removes rows with missing values (NA). drop_na()ordrop_na(SpecificColumn)
3. Analyzing Data (Summarizing & Aggregating)
After cleaning, you want to find insights. This usually involves grouping and summarizing.
Function What It Does Example (using my_data %>% ...)group_by()Groups the data by one or more columns. It doesn't change the data visually, but subsequent functions (like summarise()) will operate on each group separately.group_by(Id)summarise()(orsummarize())Collapses the data into a single row of summary statistics. Almost always used after group_by().summarise(avg_steps = mean(TotalSteps), max_calories = max(Calories))count()A handy shortcut for group_by()andsummarise(n = n()). Counts the number of rows in each group.count(Id, sort = TRUE)to see which user has the most entries.Putting It All Together: A Typical Analysis Workflow
# Find the average daily steps and total active minutes for each user, # filtering for days with at least some activity. user_summary <- daily_activity %>% select(Id, TotalSteps, VeryActiveMinutes, FairlyActiveMinutes, LightlyActiveMinutes) %>% filter(TotalSteps > 0) %>% mutate(TotalActiveMinutes = VeryActiveMinutes + FairlyActiveMinutes + LightlyActiveMinutes) %>% group_by(Id) %>% summarise( avg_daily_steps = mean(TotalSteps), avg_active_minutes = mean(TotalActiveMinutes) ) # Now view the new summary table head(user_summary)
4. Sharing Insights (Data Visualization with
ggplot2)ggplot2uses a "grammar of graphics" where you add layers to a plot.The Basic Template:
ggplot(data = <DATA>, aes(x = <X_VARIABLE>, y = <Y_VARIABLE>)) + <GEOM_FUNCTION>()Component What It Does Example ggplot(data, aes(...))Initializes the plot. Sets the data source and the aesthetics ( aes), which map variables to visual properties (like axes, color, size, etc.).ggplot(data = daily_activity, aes(x = TotalSteps, y = Calories))Geoms ( + geom_...())Adds a layer of shapes. This determines the type of plot. + geom_point()for a scatter plot.+ geom_col()for a bar chart (use for pre-summarized data).+ geom_bar()for a bar chart (counts categories for you).+ geom_line()for a line chart.Labels & Titles ( + labs())Customizes labels. Set the title, subtitle, caption, and axis labels. + labs(title = "Calories Burned vs. Steps Taken", x = "Total Steps per Day", y = "Total Calories Burned")Facets ( + facet_wrap())Creates sub-plots. Splits your plot into multiple panels based on a categorical variable. + facet_wrap(~DayOfWeek)to see a separate plot for each day.A Complete
ggplot2Example:# Create a scatter plot of Total Steps vs. Sedentary Minutes # Color the points by user ID (this will look messy, but shows the concept) ggplot(data = daily_activity, aes(x = TotalSteps, y = SedentaryMinutes)) + geom_point(alpha = 0.5) + # `alpha` makes points semi-transparent geom_smooth(method = "lm") + # Adds a linear trend line labs( title = "Relationship Between Daily Steps and Sedentary Time", x = "Total Steps", y = "Sedentary Minutes" )
Bonus: Working with Dates (
lubridatepackage)The Google Capstone projects often involve dates.
lubridate(part oftidyverse) makes this easy.Function What It Does Example as_date()Converts a column to a date format. mutate(date_col = as_date(ActivityDate, format="%m/%d/%Y"))wday(date_col, label=TRUE)Extracts the day of the week from a date. mutate(day_of_week = wday(date_col, label=TRUE))month(date_col, label=TRUE)Extracts the month from a date. mutate(month = month(date_col, label=TRUE))Keep this handy. When you encounter a task, try to frame it using these verbs: "I need to
filtermy data to only include active users, thengroup_byuser ID, and thensummariseto find their average."Good luck with the rest of the course and your capstone project! You've got this.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.