How to Use the std.error() Function in R


In statistics, the standard error of the mean is a way to measure how spread out values are in a dataset.

It is calculated as:

Standard error = s / √n

where:

  • s: sample standard deviation
  • n: sample size

One of the easiest ways to calculate the standard error of the mean in R is by using the std.error() function from the plotrix package, which is designed to perform this exact task.

The std.error() function uses the following basic syntax:

std.error(x, na.rm)

where:

  • x: A vector of numerical calculations
  • na.rm: Whether or not to remove NA values

The following example shows how to use the std.error() function in practice in R.

Note: Before using the std.error() function, you will first need to install the plotrix package. You can use the following syntax to do so:

install.packages('plotrix')              

Once the plotrix package is successfully installed, you will be able to use the std.error() function without encountering any errors.

Example: How to Use std.error() Function in R

Suppose that we create the following data frame that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                 points=c(22, 39, 24, 18, 15, 10, 28, 23),
                 assists=c(3, 8, 8, 6, 10, 14, 8, 17))

#view data frame
df

  team points assists
1    A     22       3
2    B     39       8
3    C     24       8
4    D     18       6
5    E     15      10
6    F     10      14
7    G     28       8
8    H     23      17

The dataset contains the following columns:

  • team: The team name they player belongs on
  • points: The total points scored by the player
  • assists: The total assists made by the player

Suppose that we would like to calculate the standard error of the values in the points column of the data frame.

We can use the following syntax to do so:

library(plotrix)

#calculate standard error of values in points column
std.error(df$points)

[1] 3.099179

The output tells us that the standard error of values in the points column of the data frame is 3.099179.

Note that we can also use the sapply() function to calculate the standard error of values across multiple columns of a data frame at once if we would like.

For example, we can use the following syntax to calculate the standard error of both the points and assists columns of the data frame:

library(plotrix)

#calculate standard error of values in points and assists columns
sapply(df[c('points', 'assists')], std.error)

  points  assists 
3.099179 1.566958

From the output we can see:

  • The standard error of the mean of points is 3.099179.
  • The standard error of the mean of assists is 1.566958.

Note that in this example we specified two column names to calculate the standard error for, but you can use similar syntax with the sapply() function to calculate the standard error of the mean for as many columns as you would like at once.

Also note that if you pass a character vector to the std.error() function that the function will simply return NA as a result.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Calculate Conditional Mean in R
How to Calculate a Trimmed Mean in R
How to Calculate Geometric Mean in R
How to Calculate Standard Error of the Mean in R

Leave a Reply

Your email address will not be published. Required fields are marked *