I have a data frame like that:
HLA_Status variable value
1 PP CCL24 9.645
2 PP CCL24 56.32
3 PP CCL24 7.268
4 PC CCL24 5.698
5 PC CCL24 89.457
6 PC CCL24 78.23
7 PP SPP1 23.12
8 PP SPP1 36.32
9 PP SPP1 17.268
10 PC SPP1 2.698
11 PC SPP1 9.457
12 PC SPP1 8.23
I want to reshape my data frame with reshape2::dcast() and get this:
HLA_Status CCL24 SPP1
1 PP 9.645 23.12
2 PP 56.32 36.32
3 PP 7.268 17.268
13 PC 5.698 2.698
14 PC 89.457 9.457
15 PC 78.230 8.23
But I didn't manage to do this.
I tried this:
dcast(mydt, HLA_Status ~ variable, value.var = "value")
But it didn't work.
And I see on the documentation of reshape2 that if we have multiple values per cell we need to tell dcast how to aggregate the data.
I think my problem is to don't know what to give to fun.aggregate.
How can I get the wanted data frame by using reshape2 or others packages?
This can be done with dcast
(here from data.table
) though you need a row identifier.
library(data.table)
dcast(dt, HLA_Status + rowid(HLA_Status, variable) ~ variable)
# HLA_Status HLA_Status_1 CCL24 SPP1
#1: PC 1 5.698 2.698
#2: PC 2 89.457 9.457
#3: PC 3 78.230 8.230
#4: PP 1 9.645 23.120
#5: PP 2 56.320 36.320
#6: PP 3 7.268 17.268
data
dt <- fread(" HLA_Status variable value
PP CCL24 9.645
PP CCL24 56.32
PP CCL24 7.268
PC CCL24 5.698
PC CCL24 89.457
PC CCL24 78.23
PP SPP1 23.12
PP SPP1 36.32
PP SPP1 17.268
PC SPP1 2.698
PC SPP1 9.457
PC SPP1 8.23")