# R Error in lm.fit(offset, singular.ok, …) : NA/NaN/Inf (2 Examples)

In this article, I’ll illustrate how to get rid of the “Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : NA/NaN/Inf in ‘x'” in R programming.

## Creation of Example Data

```data(iris) # Iris as example data my_iris <- iris my_iris\$Sepal.Length[c(1, 3, 5)] <- Inf # Modify iris data my_iris\$Petal.Width[c(2, 3)] <- NaN head(my_iris) # Show head of modified iris data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 Inf 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 NaN setosa # 3 Inf 3.2 1.3 NaN setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 Inf 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa```

## Example 1: Replicating the Error Message in lm.fit : NA/NaN/Inf

```lm(Sepal.Length ~ ., my_iris) # Data is not properly formatted # # Call: # lm(formula = Sepal.Length ~ ., data = my_iris) # # Coefficients: # (Intercept) Sepal.Width Petal.Length Petal.Width Speciesversicolor Speciesvirginica # 2.1580 0.4995 0.8284 -0.3162 -0.7154 -1.0143 # # Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : # NA/NaN/Inf in 'y'```

## Example 2: Solving the Error Message in lm.fit : NA/NaN/Inf

`my_iris[is.na(my_iris) | my_iris == "Inf"] <- NA # Exchange NaN & Inf by NA`
`lm(Sepal.Length ~ ., my_iris) # Apply lm to data without NaN & Inf`