0

I need to change a data.frame in R from this

Sample Protein Value
a      TBA     10
b      TBA     5
a      TBB     30
b      TBB     20

to this

Protein a  b
TBA     10 5
TBB     30 20

How do I do this? Thanks for the help!

  • 2
    you can probably use ``dcast()``. ``dcast(data, Protein ~ Sample)`` – Gainz Sep 10 '19 at 16:33
  • 1
    I suggest you take a look at some R Studio cheat sheet for such questions. Those can really help you understand those functions. You can go there : https://www.rstudio.com/resources/cheatsheets/ – Gainz Sep 10 '19 at 16:36

2 Answers2

1

Try dcast from reshape2:

df <- data.table::fread('Sample Protein Value
a      TBA     10
b      TBA     5
a      TBB     30
b      TBB     20')

dcast(df, formula = Protein ~ Sample)

Output:

  Protein  a  b
1     TBA 10  5
2     TBB 30 20
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

An option would be

library(tidyr)
spread(df1, Sample, Value)
akrun
  • 874,273
  • 37
  • 540
  • 662