Q:

How to apply combined filter criteria in a function to conduct a simple frequency analysis

Here is some data to demonstrate my problem: 
garden <- c(1,2,3,4,5,6,7,8,9,10) %>% as.numeric
apples <- c(1,0,0,0,1,0,0,NA,NA,1) %>% as.integer ()
cherries <- c(0,1,0,0,1,1,1,NA,NA,1) %>% as.integer ()
pears <- c(0,NA,1,0,0,1,1,1,0,0)  %>% as.integer ()
veg_tomatoes <- c(0,0,0,0,1,0,0,1,0,NA) %>% as.integer ()
veg_onions <- c(0,0,0,0,0,1,1,0,0,0) %>% as.integer ()
veg_peas <- c(0,0,0,0,0,0,0,0,0,1) %>%  as.integer ()
veg_beans <- c(0,0,0,0,0,0,0,1,0,1) %>% as.integer ()

garden <- data.frame(garden,apples,cherries,pears,veg_tomatoes,veg_onions,veg_peas,veg_beans)

What I would like to do is the following:
a) Count the number and percentage of gardens with only one plant (e.g. number of gardens with apple trees only, cherry trees only, etc)  
b) Count the number and percentage of gardens with either fruit tree and tomatoes  
c) Count the number and percentage of gardens with either fruit tree and one or more vegetables other than tomatoes 
I am looking for a functional solution to this problem. My idea was to start off with separate lists for fruits, vegetables and a list including all fruits and vegetables and work my way from there. However, I struggle when I try to combine the various filter criteria in an anonymous function call using lapply. For a single column I could write something like filter (garden, apples == 1, cherries == 0, pears == 0, veg_tomatoes == 0, veg_onions == 0, veg_peas == 0, veg_beans == 0) %>% nrow () for absolute numbers. But even for just one column this solution doesn't look nice, not talking about having to repeat this over and over again. Maybe someone of you has a solution of how to approach these kind of tasks and structure the functional arguments to obtain the desired frequency results.  

A:

Here is my solution using multiple filter criteria in lapply as initially desired. The solution was to call the desired column within each filter using [[x]] and combining it with another "summary" column containing the desired criterion. 
library (tidyverse)

fruits <- data.frame(garden,apples,cherries,pears)
vegs <- data.frame(garden,veg_tomatoes,veg_onions,veg_peas,veg_beans)
total <- data.frame(garden,apples,cherries,pears,veg_tomatoes,veg_onions,veg_peas,veg_beans)

fruitlist <- list("apples", "cherries", "pears")

# a) Number of gardens with one sort of plant only
plantlist <- list("apples", "cherries", "pears", "veg_tomatoes","veg_onions","veg_peas","veg_beans")
total$plants <- rowSums(total[,2:8], na.rm = TRUE)

# Select gardens with only one plant
oneplant_freq <- lapply (plantlist, function (x){
  one_plant <- total %>% filter (total[[x]] == 1 & plants == 1) %>% 
    nrow ()
})

names(oneplant_freq) <- plantlist
oneplant_freq

# b) Either sort of fruit tree & tomatoes 
vegs$other_than_tom <- rowSums(vegs[,3:5], na.rm = FALSE) # new column showing the number of vegetables other than tomatoes

# Join for fruit trees & tomatoes
tom_freq <- lapply (fruitlist, function (x){
  fruits_n_tom <- inner_join(vegs, fruits, by = "garden") %>%
    filter (veg_tomatoes == 1 & other_than_tom == 0 & fruits[[x]] == 1) %>% 
    nrow ()
})

names(tom_freq) <- fruitlist
tom_freq

# c) Either sort of fruit tree & vegetables other than tomatoes

# Join for fruit trees and other vegetables than tomatoes
other_freq <- lapply (fruitlist, function (x){
  fruits_n_others <- inner_join(vegs, fruits, by = "garden") %>%
    filter (veg_tomatoes == 0 & other_than_tom >=1 & fruits[[x]] == 1) %>% 
    nrow ()
})

names(other_freq) <- fruitlist

other_freq

With the absolute numbers now being available from the lists calculating percentages is no longer a problem. 

