0

Using R I'm trying to take common items from the Name Column and then group by that column and aggregate another column by semi-colons in a single row. I'd like to this for the column identified as FA.

From this:

Name            FAs   DS
Apple 21        180   A
Apple 21        190   D
Apple 21        100   A
Apple 11        130   A
Apple 11        110   A

To this:

 Name           FAs             DS
 Apple 21       180; 190;100    A
 Apple 11       130;110         D
steppermotor
  • 701
  • 6
  • 22

1 Answers1

1

You can try this for FAs as no clear direction is given about DS. This is similar to the awesome base R solution by @Onyambu but uses dplyr:

library(dplyr)
#Data
df <- structure(list(Name = c("Apple 21", "Apple 21", "Apple 21", "Apple 11", 
"Apple 11"), FAs = c(180L, 190L, 100L, 130L, 110L), DS = c("A", 
"D", "A", "A", "A")), row.names = c("1", "2", "3", "4", "5"), class = "data.frame")

#Code
df %>% group_by(Name) %>% summarise(FA=paste0(FAs,collapse = ';'))

# A tibble: 2 x 2
  Name     FA         
  <chr>    <chr>      
1 Apple 11 130;110    
2 Apple 21 180;190;100
Duck
  • 39,058
  • 13
  • 42
  • 84