mightaskalot
mightaskalot

Reputation: 177

Transforming an R table avoiding duplicates

i have a list of genes with an attribute associated in a 2 column table. What i want is to transform this table:

Gene    person
TMCS09g1008676  mathias
TMCS09g1008677  leonard
TMCS09g1008678  marcus
TMCS09g1008679  jan
TMCS09g1008680  jose
TMCS09g1008676  jose
TMCS09g1008677  marcus

in this:

Gene    person
TMCS09g1008676  mathias_jose
TMCS09g1008677  leonard_marcus
TMCS09g1008678  marcus
TMCS09g1008679  jan
TMCS09g1008680  jose

Someone knows a way to do it in R? Thanks in advance

Upvotes: 1

Views: 34

Answers (1)

akrun
akrun

Reputation: 887891

We can do a group by paste

library(dplyr)
df1 %>%
   group_by(Gene) %>%
   summarise(person = paste(person, collapse="_"))

Or using base R

aggregate(person ~ Gene, df1, paste, collapse='_')

Upvotes: 2

Related Questions