-3

before questioning I tried with cast, dcast, but everything went wrong... anyway, I have a data.frame like this

Genes                                               Code
 Apod Tptm      cell attachment cell to cell contact cell polarity
 Apod Serpine1  cell attachment cell to cell contact cell polarity
 Adm Ramp2                                        angiogenesis
 Adm Rdr                                          angiogenesis

My idea is to reshape the data.frame using Code column to get Unique Codes as rows and unique Genes (avoinding duplicates) jointly in each case

Code                                                  Genes
cell attachment cell to cell contact cell polarity    Apod TpTm Serpine1
angiogenesis                                          Adm Rpr Ramp2

Any idea?

Thanks

user976991
  • 411
  • 1
  • 6
  • 17
  • 1
    The community would love it if you provided your example as easily reproducible chunk'o'code. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example on how to go about this. – Roman Luštrik May 22 '14 at 16:22
  • 1
    This is actually "aggregating". The functions you should look into using with your favorite aggregation method are `strsplit` and `paste`. – A5C1D2H2I1M1N2O1R2T1 May 22 '14 at 16:25
  • Ok, ok the point is to get the unique genes unique(unlist(strsplit(c(DF[DF$Code=="ANGIOGENEIS","Genes"])," "))), my question is how to combine the reshaping with this – user976991 May 22 '14 at 16:30

1 Answers1

1

I think this will do it, too, in base R.

#sample data
test <- data.frame(Genes = c("Apod Tptm", "Apod Serpine1", "Adm Ramp2", "Adm Rdr"),
                   Code = c("X", "X", "Y", "Y"))

> test
#          Genes Code
#1     Apod Tptm    X
#2 Apod Serpine1    X
#3     Adm Ramp2    Y
#4       Adm Rdr    Y


test <- aggregate(Genes ~ Code, data=test, function(x) 
          paste(unique(unlist(strsplit(paste(x, sep=" "), " "))), collapse =" "))  

#result
> test
#  Code              Genes
#1    X Apod Tptm Serpine1
#2    Y      Adm Ramp2 Rdr
talat
  • 68,970
  • 21
  • 126
  • 157