1

I have a question about generating a dataframe out of another dataframe. The new dataframe contains the same information as the old dataframe but reported the data in an other way. The dataframe to start with, looks like this:

> a
Gene.Symbol      miRNA
    Nap1l1       mmu-let-7a
    Napepld      mmu-let-7b
    Nat8l        mmu-let-7b
    Nceh1        mmu-let-7b
    Ncoa3        mmu-let-7a
    Ndst2        mmu-let-7a
    Ndst3        mmu-let-7a
    Nedd4l       mmu-let-7a
    Nedd4l       mmu-let-7b
    Nefm         mmu-let-7a

Now i want to convert the dataframe to a dataframe like below.

>b               mmu-let-7a    mmu-let-7b
    Nap1l1       1             0 
    Napepld      0             1
    Nat8l        0             1
    Nceh1        0             1
    Ncoa3        1             0
    Ndst2        1             0
    Ndst3        1             0
    Nedd4l       1             1
    Nefm         1             0

Can anyone help me with this? Many thanks! Regards Lisanne

Lisann
  • 5,705
  • 14
  • 41
  • 50
  • 1
    Look at http://stackoverflow.com/questions/8093839/reshape-data-for-values-in-one-column. And the `reshape` or `reshape2` packages. – Thomas May 03 '13 at 07:55

1 Answers1

2

Using reshape2:

require(reshape2)
df$z <- 1
dcast(df, Gene.Symbol ~ miRNA, value.var="z", fill=0)
#   Gene.Symbol mmu-let-7a mmu-let-7b
# 1      Nap1l1          1          0
# 2     Napepld          0          1
# 3       Nat8l          0          1
# 4       Nceh1          0          1
# 5       Ncoa3          1          0
# 6       Ndst2          1          0
# 7       Ndst3          1          0
# 8      Nedd4l          1          1
# 9        Nefm          1          0
Arun
  • 116,683
  • 26
  • 284
  • 387