I'm working with a large genetics dataset. Each unique "gene" has "chains", aka groups, which I then need a count of. However, I'd also like to reduce the "gene" column into more generic groups but retain the "chain" category and combine the "count".
I've run the following to initially reduce the data:
df1 <- huge_dataset %>%
dplyr::group_by(gene) %>%
dplyr::count(chain, name="count")
which can be reproduced with this:
df1 <- data.frame("gene"= c("IGHA1", "IGHA1", "IGHA2","IGHA2", "IGHG1", "IGHG1", "IGHG2", "IGHG2"),
"chain"= c("IGK", "IGL","IGK", "IGL","IGK", "IGL", "IGK", "IGL"),
"count" = c(30, 12, 18, 19, 57, 109, 62, 34))
Then I want to combine entries in the "gene" column by the uniqueness of the fourth letter using a regex (i.e. regex--"IGH.") of each gene, and combine the "count" columns but also retain the categorization by the "chain" column. Resulting in this transformation from df1:
df2 <- data.frame("gene"= c("IGHA", "IGHA", "IGHG", "IGHG"),
"chain"= c("IGK", "IGL","IGK", "IGL"),
"count" = c(48, 31, 119, 143))
Is there a tidy/elegant solution to either
- Alter my original group_by() statement with the regex or
- Reduce df1 to df2?
I have to eventually vectorize the statement for several different gene iterations so I would rather not individually regex by each individual gene separately