0

I have a very annoying task of collecting melted data into single cells. I feel like it's very hard to explain what I need in words so here's an example:

example input:

 **ID, TAG**
id1, tag1
id1, tag2
id1, tag3
id2, tag1
id2, tag3
id3, tag2
id3, tag4

output:

**ID, TAGS**
id1, tag1 | tag2 | tag3
id2, tag1 | tag3
id3, tag2 | tag4

I hope that makes sense. So basically the tags collected into cells and separated by bars.

Thanks

EDIT:

df <- structure(list(ID = c("id1", "id1", "id1", "id2", "id2", "id3", 
"id3"), TAG = c("tag1", "tag2", "tag3", "tag1", "tag3", "tag2", 
"tag4")), row.names = c(NA, -7L), class = c("tbl_df", "tbl", 
"data.frame"))

EDIT:

For some reason

 df %>%
  group_by(TAG) %>%
  summarise(tags = paste(TAG, collapse = ", "))

did not work

This worked for me:

df %>%
  ddply(.(ID), summarise, TAGS=paste(TAG, collapse=" | "))

1 Answers1

1
library(dplyr)

df <- tribble(
    ~ID, ~TAG
    , "id1", "tag1"
    , "id1", "tag2"
    , "id1", "tag3"
    , "id2", "tag1"
    , "id2", "tag3"
    , "id3", "tag2"
    , "id3", "tag4"
)


library(dplyr)

df %>%
    group_by(ID) %>%
    summarise(TAGS = paste(TAG, collapse = " | "))

ID    TAGS              
<chr> <chr>             
1 id1   tag1 | tag2 | tag3
2 id2   tag1 | tag3       
3 id3   tag2 | tag4 
Georgery
  • 7,643
  • 1
  • 19
  • 52
  • This solution looks it would work perfectly but for some reason is doesn't for me. This is the output I get: TAGS 1 tag1 | tag2 | tag3 | tag1 | tag3 | tag2 | tag4 – MarkusGeard Feb 11 '20 at 13:17
  • @MarkusGeard: noone can help you he doesn't know your data. Please, read through this article: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – Georgery Feb 11 '20 at 14:05