I'm trying to gather data for two different variables spread over several columns each, grouped by two other variables. Here's the problem. I have several genes, several samples. Each sample has three different possible genotypes, each with an associated frequency. I want to tidy this to get a single column for gene, sample, genotype, frequency.
I have a hackjob solution to this that involves creating listcolumns, spreading those, then extracting the columns with purrr::map functions. It's ugly, not really scalable, and the frequency gets converted to a character before it gets converted back to numeric, not ideal.
Is there a better way to solve this problem?
library(tidyverse)
# or, separately load dplyr, tibble, tidyr, purrr
# Here's what I have
have <- data_frame(gene=rep(c("gX", "gY"), each=2),
sample=rep(c("s1", "s2"), 2),
genotype1=c("AA", "AA", "GG", "GG"),
genotype2=c("AC", "AC", "GT", "GT"),
genotype3=c("CC", "CC", "TT", "TT"),
freq1=c(.8,.9, .7, .6),
freq2=c(.15,.1, .2, .35),
freq3=c(.05,0, .1, .05))
have
#> # A tibble: 4 × 8
#> gene sample genotype1 genotype2 genotype3 freq1 freq2 freq3
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 gX s1 AA AC CC 0.8 0.15 0.05
#> 2 gX s2 AA AC CC 0.9 0.10 0.00
#> 3 gY s1 GG GT TT 0.7 0.20 0.10
#> 4 gY s2 GG GT TT 0.6 0.35 0.05
# Here's what I want.
# Do a multicolumn gather grouped by gene and sample
want <- have %>%
group_by(gene, sample) %>%
summarize(x1=list(c(genotype=genotype1, freq=freq1)),
x2=list(c(genotype=genotype2, freq=freq2)),
x3=list(c(genotype=genotype3, freq=freq3))) %>%
ungroup() %>%
gather(key, value, x1, x2, x3) %>%
mutate(genotype=map_chr(value, "genotype"),
freq=map_chr(value, "freq") %>% as.numeric) %>%
select(-key, -value) %>%
arrange(gene, sample, genotype)
want
#> # A tibble: 12 × 4
#> gene sample genotype freq
#> <chr> <chr> <chr> <dbl>
#> 1 gX s1 AA 0.80
#> 2 gX s1 AC 0.15
#> 3 gX s1 CC 0.05
#> 4 gX s2 AA 0.90
#> 5 gX s2 AC 0.10
#> 6 gX s2 CC 0.00
#> 7 gY s1 GG 0.70
#> 8 gY s1 GT 0.20
#> 9 gY s1 TT 0.10
#> 10 gY s2 GG 0.60
#> 11 gY s2 GT 0.35
#> 12 gY s2 TT 0.05