-3

I have two data frames df1 and df2. both of them have 240 obs. of 204 variables. I have checked and there are no missing or NA values in those files. However when I regress by this formula:

library(broom)
fit4 <- lapply(names(df1), function(x){
  dd = tidy(lm(df1[[x]] ~ df2[[x]]))
  data.frame(name = x, dd)})

It shows the following error messages:

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

dllhell
  • 1,987
  • 3
  • 34
  • 51

1 Answers1

1

As the error itself states, it might be due to NaN values or to infinite values, so you will need to delete those values or assign a zero to them.

To remove NaN from the whole data frame, you will need to create a function (as described here).

is.nan.data.frame <- function(x)
do.call(cbind, lapply(x, is.nan))

dd[is.nan(dd)] <- 0

For the infinite numbers, you can refer to this question. The two faster methods are to remove all infinite values:

dd[!rowSums(!is.finite(dd)),]

or to replace them with 0:

dd[!is.finite(dd)] <- 0
Barbara
  • 1,118
  • 2
  • 11
  • 34