Module 2: Programming and data analytics
Looking answers for ‘data analysis with r programming module 2 challenge’?
In this post, I provide accurate answers and detailed explanations for Module 2: Programming and data analytics of Course 7: Data Analysis with R Programming – Google Data Analytics Professional Certificate.
Whether you’re preparing for quizzes or brushing up on your knowledge, these insights will help you master the concepts effectively. Let’s dive into the correct answers and detailed explanations for each question.
Test your knowledge on programming concepts
Practice Quiz
1. Why do analysts use comments In R programming? Select all that apply.
- To provide names for variables
- To explain their code ✅
- To act as functions ✅
- To make an R Script more readable
Explanation:
Comments in R help clarify the purpose of code and make scripts easier to understand for others or for future reference.
2. What should you use to assign a value to a variable in R?
- A vector
- An operator ✅
- An argument
- A comment
Explanation:
In R, the assignment operator <-
is commonly used to assign values to variables.
3. Which of the following examples is the proper syntax for a function in R?
- #first
- print() ✅
- <- 20
- data_1
Explanation:
Functions in R use parentheses ()
to take arguments and perform actions, such as the print()
function.
4. Which of the following examples can you use in R for date/time data? Select all that apply.
- 2018-12-21 16:35:28 UTC ✅
- 2019-04-16 ✅
- 06:11:13 UTC ✅
- seven-24-2018
Explanation:
R can handle date/time data in standard formats like ISO 8601. The format “seven-24-2018” is not a valid date format in R.
Test your knowledge on coding in R
Practice Quiz
5. An analyst includes the following calculation in their R programming:
midyear_sales <- (quarter_1_sales + quarter_2_sales) - overhead_costs
Which variable will the total from this calculation be assigned to?
- midyear_sales ✅
- quarter_1_sales
- quarter_2_sales
- overhead_costs
Explanation:
The assignment operator <-
assigns the result of the calculation to the variable midyear_sales
.
6. An analyst is checking the value of the variable x using a logical operator, so they run the following code:
x > 35 & x < 65
Which values of x would return TRUE when the analyst runs the code? Select all that apply.
- 35
- 50 ✅
- 60 ✅
- 70
Explanation:
The condition x > 35 & x < 65
evaluates to TRUE
for values of x
that are strictly greater than 35 and strictly less than 65.
7. A data analyst inputs the following code in RStudio:
sales_1 <- 100 * sales_2
Which of the following types of operators does the analyst use in the code? Select all the apply.
- assignment ✅
- logical
- arithmetic ✅
- relational
Explanation:
The code involves an arithmetic operator for multiplication and an assignment operator for assigning the result to a variable.
Test your knowledge on R packages
Practice Quiz
8. When using RStudio, what does the installed.packages() function do?
- Installs all available packages for use in an RStudio session
- Creates code for analysts to use to edit their packages
- Presents a list of packages currently installed in an RStudio session ✅
- Selects the best packages to use based on an analyst’s current needs
Explanation:
The installed.packages()
function displays a list of all the R packages installed in the current environment.
9. In data analytics, what is CRAN?
- A collection of packages that function together to make analysis in R more efficient
- A function for finding packages to use for analysis in RStudio
- A commonly used online archive with R packages and other R resources ✅
- An R interface that has many of the same functions as RStudio
Explanation:
CRAN (Comprehensive R Archive Network) is an online repository where users can find R packages, documentation, and other resources.
10. What are ggplot2, tidyr, dplyr, and forcats all a part of?
- A collection of commonly used, CRAN-based data sets
- A list of functions that clean data efficiently
- A list of variables for use in programming in RStudio
- A collection of core tidyverse packages ✅
Explanation:ggplot2
, tidyr
, dplyr
, and forcats
are essential components of the Tidyverse, a suite of R packages designed for data science. These packages simplify tasks like data cleaning, visualization, and manipulation. The core tidyverse also includes tibble
, readr
, purrr
, and stringr
.
Test your knowledge on the tidyverse
Practice Quiz
11. When working in R, for which part of the data analysis process do analysts use the tidyr package?
- Data calculations
- Data security
- Data cleaning ✅
- Data visualization
Explanation:
The tidyr
package is part of the tidyverse and is primarily used to clean and reshape data into a tidy format, where each variable is a column, each observation is a row, and each type of observational unit forms a table.
12. Which tidyverse package contains a set of functions, such as select(), that help with data manipulation?
- readr
- ggplot2
- dplyr ✅
- forcats
Explanation:
The dplyr
package provides a suite of functions like select()
, filter()
, mutate()
, and arrange()
to manipulate data frames and tibbles efficiently in R.
13. An analyst is organizing a dataset in RStudio using the following code:
arrange(filter(Storage_1, inventory >= 40), count)
Which of the following examples is a nested function in the code?
- inventory
- count
- arrange
- filter ✅
Explanation:
A nested function is a function used as an argument within another function. Here, filter()
is nested within the arrange()
function because its output is being passed as input to arrange()
.
Module 2 challenge
Graded Quiz
14. A data analyst is assigning a variable to a value in their company’s sales dataset for 2020. Which variable name uses the correct syntax?
- -sales-2020
- 2020_sales
- sales_2020 ✅
- _2020sales
15. You want to create a vector with the values 12, 23, 51, in that exact order. After specifying the variable, what R code chunk allows you to create the vector?
- c(12, 23, 51) ✅
- v(12, 23, 51)
- c(51, 23, 12)
- v(51, 23, 12)3
Explanation:
The c()
function in R is used to create a vector. The values inside are stored in the specified order.
16. An analyst runs code to convert string data into a date/time data type that results in the following: “2020-07-10”. Which of the following are examples of code that would lead to this return? Select all that apply.
- mdy(“July 10th, 2020”) ✅
- ymd(20200710) ✅
- myd(2020, July 10)
- dmy(“7-10-2020”)
17. A data analyst inputs the following code in RStudio:
change_1 <- 70
Which of the following types of operators does the analyst use in the code?
- Assignment ✅
- Logical
- Relational
- Arithmetic
18. A data analyst is deciding on naming conventions for an analysis that they are beginning in R. Which of the following rules are widely accepted stylistic conventions that the analyst should use when naming variables? Select all that apply.
- Use single letters, such as “x” to name all variables
- Use an underscore to separate words within a variable name ✅
- Begin all variable names with an underscore
- Use all lowercase letters in variable names ✅
19. In R, what includes reusable functions and documentation about how to use the functions?
- Pipes
- Comments
- Packages ✅
- Vectors
20. Packages installed in RStudio are called from CRAN. CRAN is an online archive with R packages and other R-related resources.
- True ✅
- False
21. A data analyst is reviewing some code and finds the following code chunk:
mtcars %>%
filter(carb > 1) %>%
group_by(cyl) %>%
What is this code chunk an example of?
- Pipe ✅
- Nested function
- Vector
- Data frame
22. A data analyst finds the code mdy(10211020) in an R script. What is the year of the date that is created?
- 1021
- 1020 ✅
- 1102
- 2120
Explanation:
The mdy()
function interprets dates in the month-day-year format. Here, the year is represented as 20
, which defaults to 2120, assuming it’s part of the 21st century.
23. Which of the following is a best practice when naming R script files?
- R script file names should end in “.R” ✅
- R script file names should end in “.S”
- R script file names should end in “.rscript”
- R script file names should end in “.r-script”
24. How are base packages different from recommended packages in the R package ecosystem?
- Recommended packages are made by the community and base packages are not.
- Base packages take longer to load than recommended packages.
- Base packages are installed and loaded by default and recommended packages are not. ✅
- Recommended packages are more professionally designed than base packages.
25. Why would a data analyst want to use the CRAN network when working with RStudio?
- To add new operators to R
- To install R packages ✅
- To add pipes to R
- To install drivers to RStudio
26. A data analyst wants to take a data frame named people and filter the data where age is 10, arranged by height, and grouped by gender. Which code snippet would perform those operations in the specified order?
27. Which of the following are examples of variable names that can be used in R? Select all that apply.
- autos_5 ✅
- utility2 ✅
- 3_sales
_red_1
28. You want to create a vector with the values 43, 56, 12 in that exact order. After specifying the variable, what R code chunk lets you create the vector?
- c(43, 56, 12) ✅
- v(12, 56, 43)
- v(43, 56, 12)
- c(12, 56, 43)
29. An analyst comes across dates listed as strings in a dataset. For example, December 10th, 2020. To convert the strings to a date/time data type, which function should the analyst use?
- lubridate()
- datetime()
- now()
- mdy() ✅
30. A data analyst inputs the following code in RStudio: sales_1 <- (3500.00 * 12) Which of the following types of operators does the analyst use in the code? Select all that apply.
- Relational
- Logical
- Arithmetic ✅
- Assignment ✅
31. Which of the following files in R have names that follow widely accepted naming convention rules? Select all that apply.
- patient_details_1.R ✅
- title*123.R
- p1+infoonpatients.R
- patient_data.R ✅
32. Which of the following are included in R packages? Select all that apply.
- Naming conventions for R variable names
- Reusable R functions ✅
- Tests for checking your code ✅
- Sample datasets ✅
Explanation:
R packages contain resources like reusable functions, datasets, and testing mechanisms for code.
33. What is the name of the popular package archive dedicated to supporting R users authentic, validated code?
- The CRAN archive ✅
- The RStudio website
- The tidyverse
- Python
Explanation:
CRAN (Comprehensive R Archive Network) is an online repository that provides access to R packages and ensures they meet quality standards.
34. A data analyst writes the following code in a script and gets an error. What is wrong with their code?
penguins %>%
filter(flipper_length_mm == 200) %>%
group_by(species) %>%
summarize(mean = mean(body_mass_g)) %>%
- They are using too many functions.
- The last line should not have a pipe operator. ✅
- The first line should have a pipe operator before penguins.
- They are using the wrong characters for the pipe operator.
35. Fill in the blank: When creating a variable for use in R, your variable name should begin with _____.
- an operator
- a letter ✅
- an underscore
- a number
Explanation:
In R, variable names must start with a letter. They cannot begin with numbers, underscores, or operators.
36. You want to create a vector with the values 21, 12, 39, in that exact order. After specifying the variable, what R code chunk lets you create the vector?
- c(39, 12, 21)
- v(39, 12, 21)
- v(21, 12, 39)
- c(21, 12, 39) ✅
37. If you use the mdy() function in R to convert the string “April 10, 2019”, what will return when you run your code?
- “4.10.19”
- “4/10/2019”
- “2019-10-4”
- “2019-4-10” ✅
38. A data analyst wants to combine values using mathematical operations. What type of operator would they use to do this?
- Arithmetic ✅
- Conditional
- Logical
- Assignment
39. Which of the following files in R have names that follow widely accepted naming convention rules? Select all that apply.
- p1+infoonpatients.R
- patient_data.R ✅
- patient_details_1.R ✅
- title*123.R
Explanation:
Accepted naming conventions for R files include:
- Using lowercase letters, underscores, or numbers.
- Avoiding special characters like
*
or+
.
40. A data analyst wants to create functions, documentation, sample data sets, and code test that they can share and reuse in other projects. What should they create to help them accomplish this?
- A data frame
- A tidyverse
- A data type
- A package ✅
41. A data analyst needs a system of packages that use a common design philosophy for data manipulation, exploration, and visualization. What set of packages fulfills their need?
- Base
- CRAN
- tidyverse ✅
- Recommended
42. Which of the following are examples of variable names that can be used in R? Select all that apply.
- alpha_21 ✅
- alpha21 ✅
- tidyverse
- Recommended
43. What function is used to create vectors in the R programming language?
- v()
- c() ✅
- vector()
- combine()
44. What type of packages are automatically installed and loaded to use in R studio when you start your first programming session?
- Recommended packages
- Base packages ✅
- Community packages
- CRAN packages
45. Why would you want to use pipes instead of nested functions in R? Select all that apply.
- Pipes make it easier to add or remove functions. ✅
- Pipes make it easier to read long sequences of functions. ✅
- Nested functions are no longer supported by R.
- Pipes allow you to combine more functions in a single sequence.
Explanation:
Pipes (%>%
) improve code readability and make it simpler to modify function sequences compared to nested functions.
46. Which of the following are examples of variable names that can be used in R?
- value(2)
- value-2
- value_2 ✅
- value%2
47. A data analyst has a dataset that contains date strings like "January 10th, 2022." What lubridate function can they use to convert these strings to dates?
- myd()
- mdy() ✅
- dmy()
- ymd()
48. What is the relationship between RStudio and CRAN?
- RStudio and CRAN are both environments where data analysts can program using R code.
- CRAN creates visualizations based on an analyst’s programming in RStudio.
- CRAN contains all of the data that RStudio users need for analysis.
- RStudio installs packages from CRAN that are not in Base R. ✅
49. A data analyst previously created a series of nested functions that carry out multiple operations on some data in R. The analyst wants to complete the same operations but make the code easier to understand for their stakeholders. Which of the following can the analyst use to accomplish this?
- Pipe ✅
- Comment
- Argument
- Vector
50. A data analyst wants to assign the value 50 to the variable daily_dosage. Which of the following types of operators will they need to use in the code?
- Relational
- Arithmetic
- Assignment ✅
- Assignment
51. A data analyst needs to find a package that offers a consistent set of functions that help them complete common data manipulation tasks like selecting and filtering. What tidyverse package provides this functionality?
- tidyr
- readr
- ggplot2
- dplyr ✅
52. When programming in R, what is a pipe used as an alternative for?
- Nested function ✅
- Variable
- Installed package
- Vector
53. Which of the following is a best practice when naming functions in R?
- Function names should be capitalized
- Function names should be verbs ✅
- Function names should be very long
- Function names should start with a special character
54. A data analyst wants to create the date February 27th, 2027 using the lubridate functions. Which of the following are examples of code that would create this value? Select all that apply.
- dmy(02272027)
- mdy(“2027-02-27”)
- mdy(02272027) ✅
- ymd(“2027-02-27”) ✅
55. A data analyst inputs the following code in RStudio: print(100 / 10) What type operators does the analyst use in the code?
- Assignment
- Arithmetic ✅
- Conditional
- Logical
56. A data analyst wants to store a vector in a variable. What type of operator would they use to do this?
- Assignment ✅
- Arithmetic
- Relational
- Logical
Explanation:
In R, <-
or =
is used as an assignment operator to assign values to variables.
Related contents:
Module 1: Programming and data analytics
Module 3: Working with data in R
Module 4: More about visualizations, aesthetics, and annotations
Module 5: Documentation and reports
Module 5: Course challenge
You might also like:
Course 1: Foundations: Data, Data, Everywhere
Course 2: Ask Questions to Make Data-Driven Decisions
Course 3: Prepare Data for Exploration
Course 4: Process Data from Dirty to Clean
Course 5: Analyze Data to Answer Questions
Course 6: Share Data Through the Art of Visualization
Course 8: Google Data Analytics Capstone: Complete a Case Study