# Shapiro-Wilk Normality Test in R (Example)

In this tutorial, I’ll explain how to **perform a Shapiro-Wilk normality test** in the R programming language.

The table of content looks as follows:

Let’s do this.

## Construction of Example Data

As a first step, we need to create some data that we can use in the examples below:

set.seed(946322) # Set random seed x1 <- rnorm(100) # Create normally distributed vector x2 <- runif(100) # Create uniformly distributed vector |

set.seed(946322) # Set random seed x1 <- rnorm(100) # Create normally distributed vector x2 <- runif(100) # Create uniformly distributed vector

We can plot the exemplifying data to get a first impression of the distributions of our data by using the plot and the density functions:

plot(density(x1), ylim = c(0, 1.1), col = 2) # Draw data to density plot lines(density(x2), col = 3) legend("topleft", c("x1", "x2"), col = 2:3, lty = 1) |

plot(density(x1), ylim = c(0, 1.1), col = 2) # Draw data to density plot lines(density(x2), col = 3) legend("topleft", c("x1", "x2"), col = 2:3, lty = 1)

As revealed in Figure 1, we created a graphic containing multiple density pots with the previous R programming syntax.

The variable x1 looks normally distributed (we’ll check if this is true later). The variable x2, however, is clearly not normally distributed.

## Example: Perform Shapiro-Wilk Normality Test Using shapiro.test() Function in R

The R programming syntax below illustrates how to use the shapiro.test function to conduct a Shapiro-Wilk normality test in R.

For this, we simply have to insert the name of our vector (or data frame column) into the shapiro.test function.

Let’s check our vector x1 first:

shapiro.test(x1) # Apply shapiro.test function # Shapiro-Wilk normality test # # data: x1 # W = 0.98862, p-value = 0.5548 |

shapiro.test(x1) # Apply shapiro.test function # Shapiro-Wilk normality test # # data: x1 # W = 0.98862, p-value = 0.5548

Have a look at the previous RStudio console output of the shapiro.test function: As you can see, the p-value is larger than 0.05 meaning that our input data x1 is normally distributed.

Let’s do the same for our second variable x2:

shapiro.test(x2) # Apply shapiro.test function # Shapiro-Wilk normality test # # data: x2 # W = 0.93307, p-value = 7.464e-05 |

shapiro.test(x2) # Apply shapiro.test function # Shapiro-Wilk normality test # # data: x2 # W = 0.93307, p-value = 7.464e-05

This time the Shapiro-Wilk normality test is clearly significant, i.e. the vector x2 is not normally distributed.

## Video, Further Resources & Summary

Do you need more info on the content of this article? Then you might watch the following video of my YouTube channel. I’m explaining the R syntax of this tutorial in the video:

*The YouTube video will be added soon.*

In addition, you could have a look at the related articles which I have published on this homepage. I have released numerous posts about distributions in R already.

Summary: In this tutorial you learned how to **conduct a Shapiro-Wilk normality test** in the R programming language. If you have any additional questions, please let me know in the comments.

**5**/

**5**(

**3**votes )

### Statistics Globe Newsletter