-1

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jack Dean
  • 163
  • 1
  • 7

2 Answers2

2

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)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0

You can use dcast() from data.table.

For the input:

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"

my_data <- read.table(text = text, header = TRUE)

You can transform your data with GeneName as LHS and Species as RHS with value.var Total:

data.table::dcast(my_data,
                  GeneName ~ Species, 
                  value.var = "Total")

The result:

  GeneName Chicken Human Mouse
1     FGF1       3     6     3
clemens
  • 6,653
  • 2
  • 19
  • 31