dcast:使用复制从长到宽转换

时间:2019-06-29 17:52:21

标签: r data.table reshape dcast

我正在尝试将具有多种剂量反应强度的宽数据帧转换为多个读数,并复制为宽格式以进行拟合。

长数据如下:

ionidx=c(1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 
2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 
2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 
1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 
1L)

intensities=c(28396L, 11328L, 10496L, 32674L, 9832L, 28931L, 10580L, 30144L, 
11031L, 29720L, 30292L, 9970L, 10140L, 32286L, 31459L, 8709L, 
8332L, 31830L, 9305L, 29685L, 28933L, 11339L, 12282L, 31594L, 
12763L, 31110L, 10498L, 31405L, 32290L, 11869L, 29295L, 10743L, 
9760L, 29191L, 30020L, 8818L, 32139L, 9605L, 10668L, 31812L, 
9685L, 30365L, 8908L, 30555L, 30919L, 7640L, 8818L, 32864L, 8174L, 
30384L, 29411L, 7902L, 29947L, 8922L, 6936L, 30039L, 7798L, 8520L, 
31842L, 7008L, 28703L, 8745L, 31581L, 7600L, 28301L, 7408L, 31146L, 
12223L, 28793L, 33500L, 13359L, 31791L, 10325L, 11264L, 30491L, 
11396L, 31127L, 10655L, 32256L, 8517L, 31297L, 9000L, 30800L, 
30774L, 8845L, 7903L, 29414L, 8245L, 30337L, 8530L, 30812L, 31559L, 
8280L, 27926L, 8263L, 32300L)

dose=c(5, 5, 1.6667, 1.6667, 1.6667, 1.6667, 0.5556, 0.5556, 0.5556, 
0.5556, 0.1852, 0.1852, 0.1852, 0.1852, 0.0617, 0.0617, 0.0617, 
0.0617, 0.0617, 0.0617, 5, 5, 5, 5, 5, 5, 1.6667, 1.6667, 1.6667, 
1.6667, 0.5556, 0.5556, 0.5556, 0.5556, 0.5556, 0.5556, 0.1852, 
0.1852, 0.1852, 0.1852, 0.1852, 0.1852, 0.0617, 0.0617, 0.0617, 
0.0617, 0.0617, 0.0617, 0.0206, 0.0206, 0.0206, 0.0206, 0.0206, 
0.0206, 0.0023, 0.0069, 0.0069, 0.0069, 0.0069, 0.0023, 0.0023, 
0.0023, 0.0023, 0.0023, 0.0023, 0.0023, 0.0023, 5, 5, 5, 5, 1.6667, 
1.6667, 1.6667, 1.6667, 0.5556, 0.5556, 0.1852, 0.1852, 0.0206, 
0.0206, 0.0206, 0.0206, 0.0206, 0.0206, 0.0069, 0.0069, 0.0069, 
0.0069, 0.0069, 0.0069, 0.0069, 0.0069, 0.0023, 0.0023, 0.0023
)

resp=data.frame(ionidx=ionidx,intensities=intensities,dose=dose)

所需格式为前col=ionidx,随后的列为[dose] _ [replicate_num]

enter image description here

尝试使用dcast:

library(data.table)

dcast(setDT(resp), ionidx~dose+rowid(ionidx), value.var="intensities")

但是看起来不对。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

对于每个剂量-离子结合组合,似乎将重复数定义为沿着重复剂量数的序列。这不是您的\\s+调用中发生的情况,其中dcastrowid(ionidx)级别上创建两个序列。要重现图像中显示的输出,请尝试:

ionidx