0

I am hoping to combine two data frames,db1 and db2 which have 2 columns, One is the name of a variable called Phenotype (character) and one is the frequency. I want to combine both the dataframes in such a way that if the same phenotype exists in the other dataframe, the rows merge and the frequency increases rather than it adding stacking the dataframes one over another. I don't know how to do this on R, sp please can somebody help

db1 <- data.frame("Phenotype" = c("Intellectul Disability", "Cystic Kidney Disease", "Epilepsy", "Cardiac Disorder"), "Freq" = c(10,2,3,4))
db2 <- data.frame("Phenotype" = c("Kabuki Syndrome", "Cystic Kidney Disease", "Intellectul Disability", "Epilepsy"), "Freq" = c(13,1,0,2))
bd3 <- rbind(db1,db2)# this is what I get
bdc <- data.frame("Phenotype" = c("Intellectul Disability", "Cystic Kidney Disease", "Epilepsy", "Cardiac Disorder","Kabuki Syndrome"), "Freq" = c(10,3,5,4,13)) # this is what I am hoping to get

Thank you

user438383
  • 5,716
  • 8
  • 28
  • 43
Ar1229
  • 131
  • 2
  • 9
  • 2
    Once you combine them with `rbind` (which works fine), this is a dupe of "summarize by group": https://stackoverflow.com/q/11562656/3358272, https://stackoverflow.com/q/1660124/3358272, https://stackoverflow.com/q/12064202/3358272 (multiple functions). – r2evans Aug 28 '23 at 16:08
  • 2
    dplyr: `group_by(bd3, Phenotype) %>% summarize(Freq = sum(Freq))`; data.table: `as.data.table(bd3)[, .(Freq = sum(Freq)), by=.(Phenotype)]`; base R `aggregate(Freq ~ Phenotype, data = bd3, FUN = sum)` – r2evans Aug 28 '23 at 16:11

0 Answers0