【发布时间】:2010-12-04 17:53:42
【问题描述】:
我在重塑大型数据框时遇到了困难。而且我过去相对幸运地避免了重塑问题,这也意味着我在这方面很糟糕。
我当前的数据框看起来像这样:
unique_id seq response detailed.name treatment
a N1 123.23 descr. of N1 T1
a N2 231.12 descr. of N2 T1
a N3 231.23 descr. of N3 T1
...
b N1 343.23 descr. of N1 T2
b N2 281.13 descr. of N2 T2
b N3 901.23 descr. of N3 T2
...
我想:
seq detailed.name T1 T2
N1 descr. of N1 123.23 343.23
N2 descr. of N2 231.12 281.13
N3 descr. of N3 231.23 901.23
我查看了 reshape 包,但我不确定如何将处理因子转换为单独的列名。
谢谢!
编辑:我尝试在我的本地机器(4GB 双核 iMac 3.06Ghz)上运行它,但它一直失败:
> d.tmp.2 <- cast(d.tmp, `SEQ_ID` + `GENE_INFO` ~ treatments)
Aggregation requires fun.aggregate: length used as default
R(5751) malloc: *** mmap(size=647168) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
如果有机会,我会尝试在我们的一台更大的机器上运行它。
【问题讨论】:
-
哦,如果您遇到内存问题,您可能需要权衡空间/速度。由于您的 df 似乎是可预测的排序和分组,没有长距离依赖关系,C 风格的嵌套 for 循环可能是有序的。您必须在创建新 df 时对其进行扩展,但您不会像melt-cast 那样复制和围绕巨大的向量进行抛锚......
-
哈兰所说的。最后合并的子集可能更容易/更清洁编程。或者,也许你只是想让我们说它“没问题”,你真的“需要”8GB 的内存,你已经考虑了一段时间来运行 64 位 R ;)
-
R 中的熔化和铸造是可有效用于重塑数据的函数。用于执行此操作的函数称为 melt() 和 cast()。已在datasciencemadesimple.com/melting-casting-r 中举例说明