title: “Determine the Mean from Raw Data”
output: html_document

Using Base R

Base R comes with a variety of numerical summaries, such as mean() or median(). The syntax for the mean command is

mean(df_name)

Example 1 Computing a Population Mean and a Sample Mean

(a) Enter the data into R using the c(…) command (or load the data into R using read.csv). Then, find the population mean.

Table1 <- c(82, 77, 90, 71, 62, 68, 74, 84, 94, 88)
mean(Table1)
## [1] 79

So, \(\mu\) = 79.

(b) and (c) Now, let’s find a simple random sample of size 4 from the data in Table 1. Then, we will find the mean of the data.

set.seed(14)
Sample_Table1 <- sample(Table1,4,replace=FALSE)
Sample_Table1
## [1] 94 88 90 71
mean(Sample_Table1)
## [1] 85.75

So, \(\overline{x}\) = 85.75.

Using Mosaic

If necessary, install the Mosaic package.

install.packages("mosaic")

Mosaic also comes with a variety of numerical summaries, such as mean() or median(). The syntax for the command is

function(~variable_name,data=df_name)

The advantage of using Mosaic is that the syntax is the same for all commands. You will learn to appreciate this as you proceed through the course.

Example 1 Computing the Mean and a Sample Mean

First, we will manually enter the data in R. The Mosaic package assumes the data has column names, so we need to include the column name when building the data frame.

Table1 <- data.frame(score=c(82, 77, 90, 71, 62, 68, 74, 84, 94, 88))
Table1
##    score
## 1     82
## 2     77
## 3     90
## 4     71
## 5     62
## 6     68
## 7     74
## 8     84
## 9     94
## 10    88

(a) Find the mean of the data from Table 1 (Example 1).

library(mosaic)
mean(~score,data=Table1)
## [1] 79

So, \(\mu\) = 79.

(b) and (c) Now, let’s find a simple random sample of size 4 from the data in Table 1. Then, we will find the mean of the data.

set.seed(14)
Sample_Table1 <- sample(Table1,4,replace=FALSE)
Sample_Table1
##    score orig.id
## 9     94       9
## 10    88      10
## 3     90       3
## 4     71       4
mean(~score,data=Sample_Table1)
## [1] 85.75

So, \(\overline{x}\) = 85.75.