0

I have a data frame that that has unique names in one column with tons of variable names in a sister column. It kind of looks a little like this. Sorry it's not perfect.

df <- data.frame("Unit" = c("Unit1", "Unit1", "Unit1", "Unit2", "Unit2", "Unit3", "Unit3", "Unit3", "Unit3"),"Reg" = c("Reg1", "Reg2", "Reg3"))

The real data looks like this:

Unit_Name;Reg_Name
Unit1;Reg_ABC
Unit1;Reg_XYZ
Unit1;Reg_DEF
Unit2;Reg_ABC
Unit2;Reg_DEF
Unit3;Reg_XYZ
Unit3;Reg_ABC

What I want to get is a new column that collapses Reg_Name next to the unique Unit_Name with the Reg_Names concatenated with a semi-colon:

Unit_Name | Reg_Name
Unit_1 | Reg_ABC;Reg_XYZ;Reg_DEF
Unit_2 | Reg_DEF;Reg_ABC;Reg_DEF
Unit_3 | Reg_XYZ; Reg_ABC

I've tried twenty different solutions in R but none of them actually seem to address this exact issue and the ones that are close are dealing with different desired outcomes.

Zach
  • 1,316
  • 2
  • 14
  • 21
  • 2
    `aggregate(Reg~Unit, df, paste, collapse = ';')` – Ronak Shah May 14 '20 at 01:45
  • Geesh, there's alwasy so much to learn! After reading @ronak-shah 's amazingly concise answer, I looked thru the indexes of five R reference books; none of them listed `aggregate`. Finally found it in _R for Dummies._ Along with an explanation of the mysterious `~` notation. – David T May 14 '20 at 02:08

0 Answers0