jeudi 1 octobre 2015

catch different errors from dplyr data frame

I have a situation where my data frame can contain different errors and I want to catch both cases with an if statement afterwards.

Situtation 1: the data frame contains NA

library(dplyr)
data(iris)
attach(iris)

data <- iris %>% filter(Sepal.Length >=7.9)
   sepal_slope <- data %>% group_by(Species) %>%
   do(fit = lm(Sepal.Width ~ Sepal.Length, .)) %>%
    summarise(sepal_slope = coef(fit)[2])  

this is FALSE:

 nrow(sepal_slope) == 0
 # FALSE

is.na is TRUE here as intended

is.na(sepal_slope)
# TRUE

Situation 2: the data frame is empty

  data <- iris %>% filter(Sepal.Length >=12)
  sepal_slope <- data %>% group_by(Species) %>%
   do(fit = lm(Sepal.Width ~ Sepal.Length, .)) %>%
   summarise(sepal_slope = coef(fit)[2])  

now this is TRUE as intended:

nrow(sepal_slope) == 0
# TRUE

but this produces an error:

is.na(sepal_slope)
# sepal_slope

So I cannot use

if(nrow(sepal_slope) == 0 | is.na(sepal_slope)) sepal_slope <- 5
# Error in if (nrow(sepal_slope) == 0 | is.na(sepal_slope))    sepal_slope <- 5 : 
  argument is of length zero

How can I catch both situations in one if statement

Of course the case where sepal_slope contains a num value should be handled, if should yield TRUE here by default.

Aucun commentaire:

Enregistrer un commentaire