error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : na/nan/inf in 'y'

If you are getting the “error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : na/nan/inf in ‘y’” error message your question is how can you fix it. This message occurs when a data frame variable has NA, NaN, and Inf values and you try creating a linear model with it.

Description of the error

When doing a linear model in R programming, you use the lm function which has the format of lm(formula = a ~ b, data = dataframe). “A” is the first data frame column you want to use, and “b” is the second. “Dataframe” is the data frame to which you are applying the linear model Unfortunately, if this data frame has NA, NaN, and Inf values then you will get our error message. This is because the function can handle a missing value but not “Not a Number,” or “Infinite” values. While this is a base R function it is often used with the ggplot2 package. Here is an example of code using this function without producing our message.

> df = data.frame(A=c(1, 2, 3, 4, 5, 6, 7),
+ B=c(2, 4, 6, 8, 10, 12, 14))
>
> df
A B
1 1 2
2 2 4
3 3 6
4 4 8
5 5 10
6 6 12
7 7 14
>
> lm(B ~ A, data=df)

Call:
lm(formula = B ~ A, data = df)

Coefficients:
(Intercept) A
1.343e-15 2.000e+00

Explanation of the error

Here are two examples of code using the same example data, with non NA cases of NaN, and Inf values.

> df = data.frame(A=c(1, NA, 3, 4, 5, 6, 7),
+ B=c(2, NaN, 6, Inf, 10, 12, 14))
>
> df
A B
1 1 2
2 NA NaN
3 3 6
4 4 Inf
5 5 10
6 6 12
7 7 14
>
> lm(B ~ A, data=df)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) :
NA/NaN/Inf in ‘y’

In this example, we produce the na/nan/inf in ‘y’ version of our error message. It shows the characteristic offset offset and singular.ok singular.ok comparisons. Note that the data frame contains NA, NaN, and Inf values, and therefore it triggers the error message.

> df = data.frame(A=c(1, NA, 3, 4, 5, 6, 7),
+ B=c(2, NaN, 6, Inf, 10, 12, 14))
>
> df
A B
1 1 2
2 NA NaN
3 3 6
4 4 Inf
5 5 10
6 6 12
7 7 14
>
> lm(A ~ B, data=df)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) :
NA/NaN/Inf in ‘x ‘

In this example we produce the na/nan/inf in ‘x ‘ version of our error message. It shows the characteristic offset offset and singular.ok singular.ok comparisons. Note that the data frame has NA, NaN, and Inf values, and therefore it triggers the message. Now all that we did to create the error in lm.fit x version of the error message was to switch columns A and B in the formula argument of the lm function.

How to fix the error

Here we have two code examples using the same example data, but we fix this error by using a simple command with the format of dataframe[is.na(dataframedf) | dataframe ==”Inf”] = NA where “dataframe” is the name of the data frame being worked with. Note that this converts the NaN, and Inf values into NA values, and thereby fixes the problem. The second example is set up so that it would produce the error in lm.fit x version of this error message without this new command.

> df = data.frame(A=c(1, NA, 3, 4, 5, 6, 7),
+ B=c(2, NaN, 6, Inf, 10, 12, 14))
>
> df[is.na(df) | df==”Inf”] = NA
>
> df
A B
1 1 2
2 NA NA
3 3 6
4 4 NA
5 5 10
6 6 12
7 7 14
>
> lm(B ~ A, data=df)

Call:
lm(formula = B ~ A, data = df)

Coefficients:
(Intercept) A
0 2

Here we use the new command to fix our error message. Note that all it does is create a version of the data frame it has NA values but not any NaN, and Inf values.

> df = data.frame(A=c(1, NA, 3, 4, 5, 6, 7),
+ B=c(2, NaN, 6, Inf, 10, 12, 14))
>
> df[is.na(df) | df==”Inf”] = NA
>
> df
A B
1 1 2
2 NA NA
3 3 6
4 4 NA
5 5 10
6 6 12
7 7 14
>
> lm(A ~ B, data=df)

Call:
lm(formula = A ~ B, data = df)

Coefficients:
(Intercept) B
0.0 0.5

Here we use the new command to fix the na/nan/inf in ‘x’ version of our error message. Note that all it does is creates a version of the data frame it has NA values but not any NaN, and Inf values. This little trick is what fixes the problem.

This is an easy error message to get, particularly when you do not have control over the data. Fortunately, it is an easy one to fix regardless of which version you are getting. Once you understand it, this error message will not be a problem for you.

Scroll to top
Privacy Policy