Error: Error in, y, offset = offset, singular.ok = singular.ok, ...) : NA/NaN/Inf in 'y'

Hi, I want to add a regression line to my existing scatter plot.

i have following dataframe named "alle":

> str(alle)
'data.frame':	11 obs. of  4 variables:
 $ demo : chr  "498.300.775" "500.297.033" "502.090.235" "503.170.618" ...
 $ tot  : chr  "4.846.423" "4.891.934" "4.901.358" "4.906.313" ...
 $ besch: num  69.8 70.3 69 68.6 68.6 68.4 68.4 69.2 70.1 71.1 ...
 $ usd  : num  1.37 1.47 1.39 1.33 1.39 ...

wtih following code i tried to draw the regression line but i get errors:

> abline(lm(alle$demo~alle$tot)$coef)
Error in, y, offset = offset, singular.ok = singular.ok, ...) : 
  NA/NaN/Inf in 'y'
In addition: Warning message:
In storage.mode(v) <- "double" : NAs introduced by coercion

i know that i have no NAs(my dataframe is very small) but i checked it with isAny function too:

> any(

so how can i fix this problem? i want to draw just one regression line to my existing scatter plot :slight_smile:

I would first confirm the lm() is working with your data. I see that the demo and tot columns are of type character. To make them numeric, you need to remove the . characters. Try

alle$demo <- as.numeric(gsub("\\.", "", alle$demo))
alle$tot <- as.numeric(gsub("\\.", "", alle$tot))

Then test

FIT <- lm(alle$demo~alle$tot)

to see if you get any errors. Then

abline(reg = FIT)

should work.

1 Like

thank you so much everything worked perfectly.

now just one liitle question too: how can i put the thousand seperators in "demo" and "tot"?

I do not know of a way to include the thousands separator in a data frame and maintain the numeric type. Since thousands separators are locale dependent, it would seem risky to include them in the data frame numeric columns.
Can you explain what you are trying to achieve by having the thousands separator in those columns?

1 Like

my goal is too show the thousand seperators on my scatter plot. Just that the reader can read the numbers better.

You can use the format function to change the displayed number:
format(x, big.mark = ",")

1 Like

this works on the console but
how to display this comma seperators on the x and y axes?

If you are using base graphics, then you can use the labels option. Check ?axis for your options.

1 Like


scale_y_continuous(labels = scales::comma)

I thought you were using base graphics, rather than ggplot2, but in that case your solution is best.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.