Welcome to the community!
You can do something like this:
dataset <- data.frame(Species = c("B._lucorum_agg.", "B._pascuorum", "B._muscorum", "B._muscorum", "B._jonellus", "B._muscorum", "B._pascuorum"),
Sex = c("Worker", "Worker", "Worker", "Worker", "Worker", "Worker", "Worker"),
Behaviour = c("Foraging", "Foraging", "Foraging", "Foraging", "Foraging", "Foraging", "Foraging"),
Forage = c("T._pratense", "T._pratense", "T._pratense", "T._pratense", "C.capillaris", "C.capillaris", "L.corniculatus"),
stringsAsFactors = FALSE)
with(dataset, by(Forage, Species, table))
#> Species: B._jonellus
#>
#> C.capillaris L.corniculatus T._pratense
#> 1 0 0
#> --------------------------------------------------------
#> Species: B._lucorum_agg.
#>
#> C.capillaris L.corniculatus T._pratense
#> 0 0 1
#> --------------------------------------------------------
#> Species: B._muscorum
#>
#> C.capillaris L.corniculatus T._pratense
#> 1 0 2
#> --------------------------------------------------------
#> Species: B._pascuorum
#>
#> C.capillaris L.corniculatus T._pratense
#> 0 1 1
Created on 2019-07-08 by the reprex package (v0.3.0)
If you want to use it further, then you can convert it to a matrix using do.call(rbind, r)
, where r
is the object where you'll store the result of by
.
Also, please check the expected output you have provided. The 3rd row seems to be wrong, as T._pratense
occurs twice with B._muscorum
.
Hope this helps.