I have species data in a tidy data format. For inclusion in a report, I need to reduce the width of the table, by listing the higher orders (kingdom, phylum, class, etc) only once for each group.
I currently have:
... and need to get to something like:
... or something like:
... where each higher order is given only once, with each species within that higher order listed below.
This list is long, so needs to be script based. I have looked at using dplyr
but don't see a way to achieve this.
Below is reproducible example data if needed.
exampledata <- structure(list(KINGDOM = c("Animalia", "Animalia", "Animalia",
"Animalia", "Animalia", "Animalia", "Animalia", "Animalia", "Animalia",
"Animalia", "Animalia", "Animalia"), PHYLYM = c("Chordata", "Chordata",
"Chordata", "Chordata", "Chordata", "Chordata", "Chordata", "Chordata",
"Chordata", "Chordata", "Chordata", "Chordata"), CLASS = c("Amphibia",
"Amphibia", "Amphibia", "Amphibia", "Amphibia", "Aves", "Aves",
"Aves", "Aves", "Aves", "Aves", "Aves"), ORDER = c("Anura", "Anura",
"Anura", "Anura", "Anura", "Accipitriformes", "Ciconiiformes",
"Gruiformes", "Passeriformes", "Passeriformes", "Pelecaniformes",
"Pelecaniformes"), FAMILY = c("Ranidae", "Ranidae", "Rhacophoridae",
"Rhacophoridae", "Rhacophoridae", "Accipitridae", "Ciconiidae",
"Gruidae", "Muscicapidae", "Muscicapidae", "Threskiornithidae",
"Threskiornithidae"), SCIENTIFICNAME = c("Hylarana attigua",
"Hylarana taipehensis", "Philautus", "Polypedates leucomystax",
"Theloderma asperum", "Aviceda jerdoni", "Leptoptilos javanicus",
"Antigone antigone", "Cyanoptila cyanomelana", "Cyornis hainanus",
"Pseudibis davisoni", "Thaumatibis gigantea"), OTHERDATA = c("XYZ",
"ABC", "XYZ", "ABC", "XYZ", "XYZ", "ABC", "XYZ", "ABC", "ABC",
"XYZ", "XYZ")), row.names = c(NA, 12L), class = "data.frame")