I'm using `dplyr` to filter a data frame 

```
DF <- structure(list(Category = structure(c(1L, 1L, 1L), .Label = "A", class = "factor"), Balance = c(1000L, 3000L, 2500L)), .Names = c("Category", 
"Balance"), class = "data.frame", row.names = c(NA, -3L))

```

in the following way:

```
DF %>%
filter(Category == 'A') %>%
select(Balance) %>%
sum(., na.rm=T)   

```

It all works fine until the filter that I use returns an empty data frame, e.g. using `filter(Category == 'B')` returns

`<0 rows> (or 0-length row.names)`

When this happens I want to return 0, but my pipe breaks. I tried using `ifelse` but my syntax was incorrect:

```
DF %>%
filter(Category == 'B') %>%
select(Balance) %>% ifelse(nrow(.)==0, 0, sum(., na.rm=T))

```

Another possibility would be to use `tryCatch` but I'm not sure how to incorporate it into the pipe.

To sum up, I want to return 0 when the data frame is empty and the sum when it has other values.

Any ideas how to do it?