Search code examples
rdataframedplyrconditional-statements

How to output a default dataframe using a conditional statement with dplyr?


Let's start with this myData dataframe generated by the code immediately beneath:

  Element Group
1       C     4
2       C     1
3       D     3
4       C     8

myData <-
  data.frame(
    Element = c("C","C","D","C"),
    Group = c(4,1,3,8)
  )

The dplyr code below counts the number of instances a selected element appears in the Element column of myData, but note that the code below is searching for an element "R" that doesn't appear in myData:

library(dplyr)
rCount <- myData %>% filter(Element == 'R') %>% count(Element, name = 'counted')

Here is what is returned when rCount is run against myData:

> rCount
[1] Element counted
<0 rows> (or 0-length row.names)

How do I modify the rCount code so that the following default data frame is returned when element R (or any other unlisted element) doesn't appear in the myData dataframe, using a conditional statement with dplyr?

> rCount
  Element counted
1       R       0

Solution

  • Write a wrapper function to do the logic for you:

    myCountFunc <- function(df, filterValue) {
      x <- df %>% 
        filter(Element == filterValue) %>% 
        count(Element, name="counted")
      if (nrow(x) == 0) {
        x <- tibble(Element=filterValue, counted=0)
      } 
      x
    }
    

    So that

    myData %>% myCountFunc("R")
    # A tibble: 1 × 2
      Element counted
      <chr>     <dbl>
    1 R             0
    

    and

    myData %>%  myCountFunc("C")
      Element counted
    1       C       3