I have a dataset that is a bit too wide for the EDA plots I would like to make.
The data can be found here: https://www.massey.ac.nz/~kgovinda/data/Bill_Colour.RData
But looks something like this:
CommonName | r_ChromaticityFemaleLowerMandible | r_ChromaticityFemaleUpperMandible | r_ChromaticityMaleLowerMandible | r_ChromaticityMaleUpperMandible | many more column ... |
---|---|---|---|---|---|
African_Broadbill | 0.3331 | 0.3109 | 0.3584 | 0.3573 | etc... |
I am pivoting it longer so that three columns are tacked on to the end while removing the fourr_*
Chromaticity columns
LowerMandible | UpperMandible | Sex |
---|---|---|
0.3331 | 0.3109 | Female |
0.3584 | 0.3573 | Male |
0.3473 | 0.340 | Male |
My current method of doing this requires two pivot_longer for upper and lower and then they are bonded together.
library(tidyverse)
load(url("https://www.massey.ac.nz/~kgovinda/data/Bill_Colour.RData"))
lowerMandible <- Bill_Colour %>%
pivot_longer(c(r_ChromaticityFemaleLowerMandible, r_ChromaticityMaleLowerMandible),
values_to = "LowerMandible") %>%
select(-name)
upperMandible <- Bill_Colour %>%
select(r_ChromaticityFemaleUpperMandible,
r_ChromaticityMaleUpperMandible) %>%
pivot_longer(c(r_ChromaticityFemaleUpperMandible, r_ChromaticityMaleUpperMandible),
values_to = "UpperMandible") %>%
mutate(Sex = as.factor(str_sub(name,15,-14))) %>%
select(-name)
upperAndLowerBySex <- cbind.data.frame(lowerMandible, upperMandible) %>%
select(-r_ChromaticityFemaleUpperMandible, -r_ChromaticityMaleUpperMandible)
I feel like there should be a way to do this with one set of pivot_longer
rather than making multiple datasets.