# R Warning Message in cor() : standard deviation is zero (2 Examples)

This article explains how to **deal with the “warning message in cor(X) : the standard deviation is zero”** in the R programming language.

The tutorial will contain the following content:

So now the part you have been waiting for – the R code…

## Example 1: Reproduce the Warning Message in cor() – the standard deviation is zero

In this example, I’ll illustrate how to replicate the “warning message in cor(X) : the standard deviation is zero”.

Let’s assume that we have the two variables x1 and x2 as shown below:

x1 <- rep(1, 5) # Create first example vector x1 # Print first example vector # [1] 1 1 1 1 1 |

x1 <- rep(1, 5) # Create first example vector x1 # Print first example vector # [1] 1 1 1 1 1

y1 <- 1:5 # Create second example vector y1 # Print second example vector # [1] 1 2 3 4 5 |

y1 <- 1:5 # Create second example vector y1 # Print second example vector # [1] 1 2 3 4 5

Furthermore, let’s assume that we want to calculate the correlation of these two variables using the cor function.

Then, we might use the following R code:

cor(x1, y1) # Apply cor function # [1] NA # Warning message: # In cor(x1, y1) : the standard deviation is zero |

cor(x1, y1) # Apply cor function # [1] NA # Warning message: # In cor(x1, y1) : the standard deviation is zero

Unfortunately, the RStudio console returns the “warning message in cor(X) : the standard deviation is zero” after executing the previous R syntax.

The reason for this is that our first vector object x1 contains only one value, i.e. all elements are the same. This leads to an NA output and the previously described warning message.

So how can we debug this problem?

## Example 2: Fix the Warning Message in cor() – the standard deviation is zero

Example 2 explains how to handle the “warning message in cor(X) : the standard deviation is zero”.

For this, we could modify our two vector objects:

x2 <- c(rep(1, 5), 2) # Modify first example vector x2 # Print first modified example vector # [1] 1 1 1 1 1 2 |

x2 <- c(rep(1, 5), 2) # Modify first example vector x2 # Print first modified example vector # [1] 1 1 1 1 1 2

y2 <- 1:6 # Modify second example vector y2 # Print second modified example vector # [1] 1 2 3 4 5 6 |

y2 <- 1:6 # Modify second example vector y2 # Print second modified example vector # [1] 1 2 3 4 5 6

Now, we could apply the cor function to our updated variables:

cor(x2, y2) # Apply cor function # [1] 0.6546537 |

cor(x2, y2) # Apply cor function # [1] 0.6546537

As you can see, a valid output is returned and no warning messages are shown.

Note that updating the data as shown in this example usually doesn’t make sense from a theoretical viewpoint. In case this warning message occurs, you should investigate why one of your variables contains only one specific value.

Maybe you have performed a previous data manipulation step wrong, or maybe your data is just not suited for the calculation of a correlation coefficient.

## Video & Further Resources

Have a look at the following video on the Statistics Globe YouTube channel. I illustrate the R programming codes of this tutorial in the video:

*The YouTube video will be added soon.*

In addition, you might have a look at the related articles on this homepage:

- Warning Message in read.table: Incomplete Final Line Found by readTableHeader
- Warning Message in R: argument is not numeric or logical: returning NA
- Warning Message: Number of items to Replace is not Multiple of Length
- ggplot2 Warning Message – Use of data$X is discouraged. Use X instead
- Warnings & Errors in R
- R Programming Overview

To summarize: In this article, I have explained how to **avoid the “warning message in cor(X) : the standard deviation is zero”** in the R programming language. Don’t hesitate to let me know in the comments section below, if you have additional questions. Furthermore, don’t forget to subscribe to my email newsletter to get regular updates on the newest tutorials.