LucasDan712
LucasDan712

Reputation: 3

I'm getting a "missing value" error when running this for/if code in R

I'm trying to get rid of the rows in which indeXLE$XLE == 0 by running this code:

for (i in 1:6975) {
  if (indeXLE$XLE[i] == 0) indeXLE <- indeXLE %>% slice(-i)
}

But I'm getting the "missing value where TRUE/FALSE needed" error. Any thoughts?

Upvotes: 0

Views: 44

Answers (1)

Kel Varnsen
Kel Varnsen

Reputation: 324

Try using the filter function from the dplyr package.

#install.packages("dplyr")
library(dplyr)

indeXLE <- filter(indeXLE, !is.na(XLE) & XLE != 0)

Upvotes: 1

Related Questions