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