I need to **sum the overlaps between three dataframes and repeat this action multiple times.**

To be more specific, I want to count the overlaps between where insects forage, the plants they forage on, and where those plants are found.

So far I can do this, using indexing, for just one insect species out of a list of species, but I need to do it for multiple species (this will later be done on far more species and plant families so I don't want to do it by hand).

**Example dataframes:**

Plant habitats (location):

```
plantloc <- data.frame(matrix(ncol=5,nrow=5, dimnames=list(NULL, c("plantfamily", "hedge", "margin", "arablefields", "grassfields"))))
plantloc$plantfamily <- c("Aceraceae", "Boraginaceae", "Dipsacaceae", "Lamiaceae", "Umbelliferae")
plantloc$hedge <- c(1,1,1,1,1)
plantloc$margin <- c(0,1,0,1,1)
plantloc$arablefields <- c(0,1,0,1,1)
plantloc$grassfields <- c(0,1,1,1,1)

```

Bumblebee forage plants:

```
bbfpl <-data.frame(matrix(ncol=6,nrow=14, dimnames=list(NULL, c("species","Aceraceae", "Boraginaceae", "Dipsacaceae", "Lamiaceae", "Umbelliferae"))))
bbfpl$species <- c("Bombus_distinguendus","Bombus_hortorum","Bombus_humilis","Bombus_jonellus","Bombus_lapidarius","Bombus_lucorum","Bombus_muscorum","Bombus_pascuorum","Bombus_pratorum","Bombus_ruderarius","Bombus_ruderatus","Bombus_soroeensis","Bombus_sylvarum","Bombus_terrestris")
bbfpl$Aceraceae <- c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)
bbfpl$Boraginaceae <- c(0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1)
bbfpl$Dipsacaceae <- c(1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1)
bbfpl$Lamiaceae <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1)
bbfpl$Umbelliferae <- c(1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0)

```

Bumblebee foraging habitats:

```
bbfhab <- data.frame(matrix(ncol=5,nrow=14, dimnames=list(NULL, c("species", "hedge", "margin", "arablefields", "grassfields"))))
bbfhab$species <- c("Bombus_distinguendus","Bombus_hortorum","Bombus_humilis","Bombus_jonellus","Bombus_lapidarius","Bombus_lucorum","Bombus_muscorum","Bombus_pascuorum","Bombus_pratorum","Bombus_ruderarius","Bombus_ruderatus","Bombus_soroeensis","Bombus_sylvarum","Bombus_terrestris")
bbfhab$hedge <- c(0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1)
bbfhab$margin <- c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
bbfhab$arablefields <- c(0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1)
bbfhab$grassfields <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

```

**Using Bombus\_sylvarum as an example:**

1. Match the plant families used by Bombus\_sylvarum (i.e. the column names of *bbfpl* where B\_sylv has a 1) with the plant family column of *plantloc*, and return the row index of those plant families

```
    match(colnames(bbfpl)[which(bbfpl[13,] == 1)],plantloc$plantfamily)

```

2. Match the foraging habitats used by Bombus\_sylvarum (i.e. the column names of *bbfhab* where B\_sylv has a 1) with the habitat columns of *plantloc*, and return the column index of those habitats

```
    match(colnames(bbfhab)[which(bbfhab[13,] == 1)],colnames(plantloc))

```

3. Use the two matches above to index the habitats (columns) and plant families (rows) in *plantloc* that are used by Bombus\_sylvarum, and sum all values in these indexed locations. This gives the total number of overlaps. In this case it's 8.

```
    sum(plantloc[match(colnames(bbfpl)[which(bbfpl[13,] == 1)],plantloc$plantfamily) , match(colnames(bbfhab)[which(bbfhab[13,] == 1)],colnames(plantloc))])

```

I can do each individual step for all 14 species, e.g.:

```
apply(bbfpl, 1,
      function(x){
      match(colnames(bbfpl)[which(x==1)],plantloc$plantfamily)
      })

```

but I can't work out how to put both steps together.

**Question:**

How do I do this for all 14 bumblebee species and put the results in a new dataframe (first column = bumblebee species, second column = result)?

lapply? loop? purrr?

I have scoured similar questions but haven't managed to solve this.

Thank you.