Search code examples
rdataframebiomart

How can I use R to change my table


Using R how would I change my table from this:

GeneID    GeneName    Species    Paralogues    Domains    Total
 1234      FGF1        Human         4            2         6
 5678      FGF1        Mouse         2            1         3
 9104      FGF1       Chicken        3            0         3

To a table that represents the total column e.g.

GeneName    Human    Mouse    Chicken
  FGF1        6        3         3

Solution

  • You can use dplyr::spread to reshape from long to wide:

    library(tidyverse);
    df %>% 
        select(GeneName, Species, Total) %>% 
        spread(Species, Total)
    #  GeneName Chicken Human Mouse
    #1     FGF1       3     6     3
    

    Sample data

    df <- read.table(text =
        "GeneID    GeneName    Species    Paralogues    Domains    Total
     1234      FGF1        Human         4            2         6
     5678      FGF1        Mouse         2            1         3
     9104      FGF1       Chicken        3            0         3", header  = T)