Home > Back-end >  How do merge two data frames in R with different number of rows In R based on a column but the repea
How do merge two data frames in R with different number of rows In R based on a column but the repea

Time:09-13

These are the 2 example data frames

s1=data.frame(id=c(1,2,3,4),sex=c("M","M,","F","F"))
s2=data.frame(id=c(1,1,2,2,2,3,4),symp=c("cold","fever","headache","pain","cough","sneeze","cramps"))

I want to combine this in such a way that it looks like

s3=data.frame(id=c(1,2,3,4),sex=c("M","M,","F","F"), symp=c("cold,fever","headache,pain,cough","sneeze","cramps"))

CodePudding user response:

One way would be to simply join the data frames as usual and then use group_by with mutate like this

library(tidyverse)
s4 <- left_join(s1, s2, by = "id")
s5 <- 
  s4 |> 
  group_by(id, sex) |> 
  mutate(
    symp = paste0(symp, collapse = ",")
  ) |> 
  unique()

Note that you may have to replace the pipe |> with %>%.

  •  Tags:  
  • r
  • Related