data.table r;如何通过fwrite保留列表元素名称?
我有以下数据列表。
l[1:2]
$cluster1
rn p_val avg_log2FC pct.1 pct.2 p_val_adj
<char> <num> <num> <num> <num> <num>
1: Adamts20 1.438101e-65 1.030705 0.001 0.118 4.314302e-62
2: Gal 3.233897e-54 16.358484 0.006 0.208 9.701690e-51
3: Hrnr 1.295286e-51 14.374645 0.007 0.182 3.885858e-48
4: Ngb 4.143060e-44 1.925631 0.021 0.240 1.242918e-40
5: Svep1 3.872262e-41 8.232291 0.283 0.582 1.161679e-37
---
366: Cyfip2 1.468792e-05 2.417948 0.056 0.118 4.406375e-02
367: Fcgr2b 1.505992e-05 -4.719973 0.231 0.148 4.517976e-02
368: Ogn 1.538930e-05 9.708295 0.242 0.339 4.616791e-02
369: Lrrc15 1.549036e-05 -2.966498 0.241 0.352 4.647109e-02
370: AY036118 1.624054e-05 2.602963 0.715 0.788 4.872162e-02
$cluster2
rn p_val avg_log2FC pct.1 pct.2 p_val_adj
<char> <num> <num> <num> <num> <num>
1: Samd13 1.841651e-127 8.7785032 0.001 0.144 5.524952e-124
2: Gata1 2.210414e-99 17.0522687 0.002 0.126 6.631242e-96
3: Col4a4 1.250827e-58 9.6145811 0.583 0.254 3.752481e-55
4: Bsx 4.028430e-54 18.2214242 0.202 0.568 1.208529e-50
5: Gimap3 4.650536e-53 -1.6106639 0.017 0.255 1.395161e-49
---
217: Cd3e 1.452524e-05 18.6983055 0.131 0.175 4.357573e-02
218: Selenom 1.455810e-05 0.9648215 0.293 0.201 4.367429e-02
219: A330069E16Rik 1.469167e-05 -5.4387846 0.388 0.282 4.407501e-02
220: Lypd1 1.560834e-05 -2.1249661 0.176 0.100 4.682501e-02
221: Alad 1.633998e-05 -1.1929284 0.226 0.175 4.901995e-02
我想通过FWRITE将此列表导出到CSV文件。我运行以下代码;
lapply(l[1:2], function(x) fwrite(as.data.table(x), "DGE.csv"))
由此产生的CSV文件没有列表元素名称,$ cluster1和$ cluster。是否有任何方法可以将列表元素名称作为每个元素列表和列标题的标题作为按下所需的输出?
cluster1
rn p_val avg_log2FC pct.1 pct.2 p_val_adj
Samd13 1.84E-127 8.778503219 0.001 0.144 5.52E-124
Gata1 2.21E-99 17.05226868 0.002 0.126 6.63E-96
Col4a4 1.25E-58 9.614581115 0.583 0.254 3.75E-55
Bsx 4.03E-54 18.22142419 0.202 0.568 1.21E-50
Gimap3 4.65E-53 -1.61066387 0.017 0.255 1.40E-49
Gm26825 9.54E-51 12.96067221 0.341 0.596 2.86E-47
Erfe 3.72E-48 11.74946067 0.007 0.156 1.12E-44
cluster2
rn p_val avg_log2FC pct.1 pct.2 p_val_adj
Samd13 1.63E-12 15.05539372 0.038 0.141 4.90E-09
Frmd5 1.75E-12 -1.28277874 0.154 0.318 5.26E-09
Pla2g2f 2.06E-12 -1.51234633 0.142 0.056 6.17E-09
Aldh1a2 2.25E-12 5.127605606 0.274 0.426 6.75E-09
Fxyd2 2.51E-12 5.246386986 0.626 0.805 7.53E-09
Adam30 3.15E-12 1.53746735 0.203 0.362 9.44E-09
Gpr35 3.15E-12 -3.18282304 0.691 0.516 9.46E-09
Il10rb 3.65E-12 1.473277609 0.42 0.598 1.10E-08
Gk5 3.72E-12 -14.0221264 0.177 0.074 1.12E-08
.......
I have a following list of data.tables;
l[1:2]
$cluster1
rn p_val avg_log2FC pct.1 pct.2 p_val_adj
<char> <num> <num> <num> <num> <num>
1: Adamts20 1.438101e-65 1.030705 0.001 0.118 4.314302e-62
2: Gal 3.233897e-54 16.358484 0.006 0.208 9.701690e-51
3: Hrnr 1.295286e-51 14.374645 0.007 0.182 3.885858e-48
4: Ngb 4.143060e-44 1.925631 0.021 0.240 1.242918e-40
5: Svep1 3.872262e-41 8.232291 0.283 0.582 1.161679e-37
---
366: Cyfip2 1.468792e-05 2.417948 0.056 0.118 4.406375e-02
367: Fcgr2b 1.505992e-05 -4.719973 0.231 0.148 4.517976e-02
368: Ogn 1.538930e-05 9.708295 0.242 0.339 4.616791e-02
369: Lrrc15 1.549036e-05 -2.966498 0.241 0.352 4.647109e-02
370: AY036118 1.624054e-05 2.602963 0.715 0.788 4.872162e-02
$cluster2
rn p_val avg_log2FC pct.1 pct.2 p_val_adj
<char> <num> <num> <num> <num> <num>
1: Samd13 1.841651e-127 8.7785032 0.001 0.144 5.524952e-124
2: Gata1 2.210414e-99 17.0522687 0.002 0.126 6.631242e-96
3: Col4a4 1.250827e-58 9.6145811 0.583 0.254 3.752481e-55
4: Bsx 4.028430e-54 18.2214242 0.202 0.568 1.208529e-50
5: Gimap3 4.650536e-53 -1.6106639 0.017 0.255 1.395161e-49
---
217: Cd3e 1.452524e-05 18.6983055 0.131 0.175 4.357573e-02
218: Selenom 1.455810e-05 0.9648215 0.293 0.201 4.367429e-02
219: A330069E16Rik 1.469167e-05 -5.4387846 0.388 0.282 4.407501e-02
220: Lypd1 1.560834e-05 -2.1249661 0.176 0.100 4.682501e-02
221: Alad 1.633998e-05 -1.1929284 0.226 0.175 4.901995e-02
I'd like to export this list to a csv file by fwrite. I run this following code;
lapply(l[1:2], function(x) fwrite(as.data.table(x), "DGE.csv"))
A resulting csv file doesn't have list element names, $cluster1 and $cluster. Are there any ways to add list element names as the header of each of the element list and column headers as a follow desired output by using fwrite ?;
cluster1
rn p_val avg_log2FC pct.1 pct.2 p_val_adj
Samd13 1.84E-127 8.778503219 0.001 0.144 5.52E-124
Gata1 2.21E-99 17.05226868 0.002 0.126 6.63E-96
Col4a4 1.25E-58 9.614581115 0.583 0.254 3.75E-55
Bsx 4.03E-54 18.22142419 0.202 0.568 1.21E-50
Gimap3 4.65E-53 -1.61066387 0.017 0.255 1.40E-49
Gm26825 9.54E-51 12.96067221 0.341 0.596 2.86E-47
Erfe 3.72E-48 11.74946067 0.007 0.156 1.12E-44
cluster2
rn p_val avg_log2FC pct.1 pct.2 p_val_adj
Samd13 1.63E-12 15.05539372 0.038 0.141 4.90E-09
Frmd5 1.75E-12 -1.28277874 0.154 0.318 5.26E-09
Pla2g2f 2.06E-12 -1.51234633 0.142 0.056 6.17E-09
Aldh1a2 2.25E-12 5.127605606 0.274 0.426 6.75E-09
Fxyd2 2.51E-12 5.246386986 0.626 0.805 7.53E-09
Adam30 3.15E-12 1.53746735 0.203 0.362 9.44E-09
Gpr35 3.15E-12 -3.18282304 0.691 0.516 9.46E-09
Il10rb 3.65E-12 1.473277609 0.42 0.598 1.10E-08
Gk5 3.72E-12 -14.0221264 0.177 0.074 1.12E-08
.......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编写单独文件的替代方法:将列表名称作为列附加,然后将
rbindlist
它们编写,然后写下它们,然后在该列名上读取它们时,split
。示例数据:
附加,组合,写入:
结果文件,任何香草CSV读取器都可以读取:
读取并恢复原始对象格式:
An alternative to writing separate files: append the list-name as a column, then
rbindlist
them, write them, and when you read them thensplit
on that column name.Sample data:
Append, combine, write:
The resulting file, that can be read by any vanilla CSV reader:
Reading it in and restoring the original object format: