Chapter 2: Descriptive Statistics Appendix Descriptive Statistics with R
Book Title: Business Analytics
Printed By: Kathleen Lloyd ([Link]@[Link])
© 2021 Cengage Learning, Cengage Learning
Chapter Review
Appendix Descriptive Statistics with R
In this appendix, we describe how to use R to compute a variety of descriptive statistics and
to display boxplots. We then show how R can be used to obtain covariance and correlation
measures for two variables.
Descriptive Statistics
Table 3.1 provides starting monthly salaries for the 12 business school graduates. These
data are in the file StartingSalaries. The following steps can be used to generate descriptive
statistics for the starting salary data in R.
Note the name of the monthly salary data
column here is [Link]. Salary….
This is because the name of this column in
the .csv file is “Monthly Starting Salary ($)”
and R replaces the blanks and non-
alphanumeric characters with periods in the
name for the column within the data frame.
Step 1.
As outlined in the Appendix: Getting Started with R and RStudio, change the
working directory to where the file [Link] is located.
Step 2.
To read the file and create a data frame, enter the following in the Console
pane of RStudio:
Step 3.
Enter the following to extract a vector of the monthly starting salaries from the
data frame:
Step 4.
A summary of the descriptive statistics for the monthly salary data is created
by entering the command:
These steps create the following output in the Console pane of RStudio.
This output indicates that
the minimum monthly If your output in R is not showing decimal
salary is $5,710, the first values, you can change the number of digits
quartile of the monthly displayed by using the command
salary data is $5,872.50, options(digits=10)
the median is $5,905, the
mean is $5,940, the third
quartile is $5,975, and the
maximum value is $6,325.
We can generate a five-number summary that computes the minimum, the
first quartile, the median, the third quartile, and the maximum values for the
data in R by entering the command:
which produces the output
There are many different ways to compute percentiles and quartiles. In fact,
the method used to compute quartiles in R with the fivenum command is
different than the method used to compute quartiles with the summary
command. This is why we see slightly different values produced for the first
and third quartiles using the fivenum command versus the summary
command. The method used to compute quartiles with the [Link]
(or [Link] for percentiles) Excel command is different from both
of these default methods in R. Therefore, the values computed in Excel using
the [Link] (or PERCENTILE. EXC) command can be different than
both of these quartile values computed in R. R allows for nine different ways
(defined as “types”) of calculating percentiles and quartiles. You can control
this explicitly using the R function quantile. For instance, R’s type=6
approach to calculating percentiles matches that used by the Excel function
[Link]. Therefore, entering the command
produces the output
which matches the value produced in Excel using the function
=PERCENTILE(B2:B13,25) [or =[Link](B2:B13,1)] for the
StartingSalaries data.
We can also calculate the mean and median for the starting salary data
directly in R using the functions mean and median, respectively. We can
calculate the standard deviation for the monthly salary data by entering
in the Console pane which produces the output
indicating that the standard deviation of these data is 165.65.
Boxplots
R can be used to create a boxplot for the [Link] data through the following
steps. Note that Steps 1 through 3 can be skipped if you have already loaded the starting
salaries data as a data frame and extracted the monthly starting salaries.
Step 1.
As outlined in the Appendix: Getting Started with R and RStudio, change the
working directory to where the file [Link] is located.
Step 2.
To read the file and create a data frame, enter the following in the Console
pane of RStudio:
Step 3.
Enter the following to extract a vector of the monthly starting salaries from the
data frame:
Step 4.
The boxplot shown in Figure R1 is created in R using the following command
where xlab= provides a label for the horizontal (x) axis and
horizontal=TRUE changes the orientation of the boxplot from the default
vertical to horizontal.
Figure R1
Boxplot Created in R for Monthly Starting Salary Data
R can also be used to create comparative boxplots. We demonstrate this using the data in
the file [Link], which contains the major and starting salary data for a sample of
111 recent business school graduates. We use the following steps.
Step 1.
As outlined in the Appendix: Getting Started with R and RStudio, change the
working directory to where the file [Link] is located.
Step 2.
To read the file and create a data frame, enter the following in the Console
pane of RStudio:
Step 3.
Enter the following to create the comparative boxplots in the Plots pane of
RStudio as shown in Figure R2:
Figure R2
Comparative Boxplots Created in R for the Monthly Starting
Salary by Major Data
In the preceding command, [Link]…. ∼ Major,
data = major_salaries_df indicates that we are using
[Link]…. to define the boxplots, there will be a separate
boxplot for each Major, and the data are being drawn from the
major_salaries_df data frame. The arguments xlab= and ylab= provide the
labels for the x-axis and y-axis, respectively.
Covariance and Correlation
To illustrate the calculation of covariance and correlation coefficient in R, consider the
advertising/sales relationship for an electronics store in San Francisco. On 10 occasions
during the past three months, the store used weekend television commercials to promote
sales at its stores. The managers want to investigate whether a relationship exists between
the number of commercials shown and sales at the store during the following week. Sample
data for the 10 weeks with sales in hundreds of dollars are contained in the file
[Link]. The following steps show how R can be used to compute the covariance
and correlation of two variables—number of commercials and sales volume—using the file
[Link]. Note that Steps 1 through 4 can be skipped if you have already loaded the
electronics store data as a data frame in R and extracted the vectors for number of
commercials and sales volume.
Step 1.
As outlined in the Appendix: Getting Started with R and RStudio, change the
working directory to where the file [Link] is located.
Step 2.
To read the file and create a data frame, enter the following in the Console
pane of R Studio:
Step 3.
Enter the following to extract the vector of the number of commercials from
the data frame:
Step 4.
Enter the following to extract the vector of the sales volume from the data
frame:
Step 5.
To calculate the sample covariance between the number of commercials and
the sales volume, we use the command:
which provides the output
in the Console pane, indicating the sample covariance is 11.
Step 6.
To calculate the correlation coefficient between the number of commercials
and the sales volume, we use the command:
which provides the output
in the Console pane, indicating that the correlation coefficient is 0.93.
Chapter 2: Descriptive Statistics Appendix Descriptive Statistics with R
Book Title: Business Analytics
Printed By: Kathleen Lloyd ([Link]@[Link])
© 2021 Cengage Learning, Cengage Learning
© 2025 Cengage Learning Inc. All rights reserved. No part of this work may by reproduced or used in any form or by any means - graphic, electronic, or mechanical, or in any other manner - without the written permission of the copyright holder.