1

This may be a newbie question, but I've searched everywhere and can't find a way to do it. I have a data frame in R that looks like this:

Target Sample Regulation
AKT1    00h    1.00000
AKT1    02h    1.27568
AKT1    06h   -1.29813
AKT1    12h    1.12357
AKT1    48h    1.02284
AKT2    00h    1.00000
AKT2    02h    1.08692
AKT2    06h    1.19489
AKT2    12h   -1.07677
AKT2    48h   -1.18955

data$Target and data$Sample are class=factor

I need to create a table to look like this:

Target/Sample  AKT1     AKT2
00h            1.00000  1.00000
02h            1.27568  1.08692
06h           -1.29813  1.19489
12h            1.12357 -1.07677
48h            1.02284 -1.18955

In other words, I need to create a new data frame where the columns are data$Target levels, the rows are data$Sample levels, and populate it with the corresponding values in data$Regulation.

This is what I could come up with:

newdata <- data.frame(Time=levels(data$Sample),
AKT1=as.numeric(data$Regulation[which(dat$Target=="AKT1",)]))

but of course I don't want to go one by one since dat$Target has >100 levels (genes). Help please!

Thank you all so much!

user3803664
  • 11
  • 1
  • 2

2 Answers2

0

You need a conversion from long to wide format. Here is an alternative using reshape:

reshape(df, idvar = "Sample", timevar = "Target", direction = "wide")
  Sample Regulation.AKT1 Regulation.AKT2
1    00h         1.00000         1.00000
2    02h         1.27568         1.08692
3    06h        -1.29813         1.19489
4    12h         1.12357        -1.07677
5    48h         1.02284        -1.18955
DatamineR
  • 10,428
  • 3
  • 25
  • 45
0

We could use tidyr

library(tidyr)
spread(df1, Target, Regulation)
#  Sample     AKT1     AKT2
#1    00h  1.00000  1.00000
#2    02h  1.27568  1.08692
#3    06h -1.29813  1.19489
#4    12h  1.12357 -1.07677
#5    48h  1.02284 -1.18955
akrun
  • 874,273
  • 37
  • 540
  • 662