0

I have a set of data:

 Project<-c("1","2","3","4","5")
 Product<-c("A","B","A","B","B")
 data<-data.frame(Product, Project)

 > data
   Product Project
         A       1
         B       2
         A       3
         B       4
         B       5

I want to group the data such that it becomes:

    Product Project
       A       1,3
       B       2,4,5

May I ask how can I do this in R? I was looking up the apply function but still couldn't figure out how I can do this..... Any help would be appreciated.

Head and toes
  • 659
  • 3
  • 11
  • 29

1 Answers1

1

One solution with dplyr:

library(dplyr)
data %>%
  group_by(Product) %>%
  summarise(Project = paste(Project, collapse = ','))
## A tibble: 2 x 2
#  Product Project
#   <fctr>   <chr>
#1       A     1,3
#2       B   2,4,5
LyzandeR
  • 37,047
  • 12
  • 77
  • 87