1

old problem, for new one see below

I have a data.frame

df<-data.frame("name"  = c("A","A","B","C"), 
               "class" = c("ab","cd","cd","ef"),
               "type"  = c("alpha","beta","gamma","delta"))

> df
  name class  type
1    A    ab alpha
3    A    ab  beta
4    B    cd gamma
5    C    ef delta

so name A has both type alpha and beta and appears both with

I want my data frame to look like this (type column may contain one long string separated by comma):

> df
  name class  type
1    A    ab alpha, beta
2    B    cd gamma
3    C    ef delta

what did not work was dcast(df, name~type)

any suggestions?

New Problem

I want name to be the decisive selector. So A has class ab with type alpha and class cd with types alpha and beta.

df<-data.frame("name"  = c("A","A","A","B","C"), 
               "class" = c("ab","cd","cd","cd","ef"),
               "type"  = c("alpha","alpha","beta","gamma","delta"))

> df
  name class  type
1    A    ab alpha
2    A    cd alpha
3    A    cd  beta
4    B    cd gamma
5    C    ef delta

dplyr::summarise(var = paste(type, collapse = ", "))` (see solution below) returns

> df
  name var
1    A alpha, alpha, beta
2    B gamma
3    C delta

This causes a double alpha in the first row. I am looking for a possibilty to remove this doublet. Target:

> df
  name var
1    A alpha, beta
2    B gamma
3    C delta

edit:

solved by Gregor, see comments

enroute
  • 183
  • 9
  • `melt` and `dcast` are used when you (melt) need to convert multiple columns to two columns (key column and value column), or (dcast) when you need to convert two key/value columns into multiple columns. Here, you are keeping the same columns so you're not using `melt` or `dcast`. – Gregor Thomas Sep 25 '18 at 13:14
  • 1
    Regarding your new problem, you can use the existing answer just change `paste(type, ...)` to `paste(unique(type), ...)`. In general, it's bad form to edit a question and change the requirements after getting a satisfactory answer to the original question. It's usually better to just open a new question. – Gregor Thomas Sep 26 '18 at 14:50
  • your solutions works very well @Gregor, thank you! – enroute Sep 27 '18 at 08:28

1 Answers1

3

Try this. We group by name and class, then collapse with a comma:

library(dplyr)

df %>%
  group_by(name, class) %>%
  summarise(type = paste(type, collapse = ","))
#> # A tibble: 3 x 3
#> # Groups:   name [?]
#>   name  class type      
#>   <fct> <fct> <chr>     
#> 1 A     ab    alpha,beta
#> 2 B     cd    gamma     
#> 3 C     ef    delta

Created on 2018-09-25 by the reprex package (v0.2.0).

AndS.
  • 7,748
  • 2
  • 12
  • 17