list 将数据框转换为列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18923518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert data frame to list
提问by dayne
I am trying to go from a data frame to a list structure in R (and I know technically a data frame is a list). I have a data frame containing reference chemicals and their mechanisms different targets. For example, estrogen is an estrogen receptor agonist. What I would like is to transform the data frame to a list, because I am tired of typing out something like:
我正在尝试从数据框到 R 中的列表结构(我知道从技术上讲数据框是一个列表)。我有一个包含参考化学品及其机制不同目标的数据框。例如,雌激素是一种雌激素受体激动剂。我想要的是将数据框转换为列表,因为我厌倦了输入以下内容:
refchem$chemical_id[refchem$target=="AR" & refchem$mechanism=="Agonist"]
every time I need to access the list of specific reference chemicals. I would much rather access the chemicals by:
每次我需要访问特定参考化学品的列表时。我更愿意通过以下方式获取化学品:
refchem$AR$Agonist
I am looking for a general answer, even though I have given a simplified example, because not all targets have all mechanisms.
我正在寻找一个通用的答案,尽管我给出了一个简化的例子,因为并非所有的目标都有所有的机制。
This is really easy to accomplish with a loop:
这很容易用循环来完成:
example <- data.frame(target=rep(c("t1","t2","t3"),each=20),
mechan=rep(c("m1","m2"),each=10,3),
chems=paste0("chem",1:60))
oneoption <- list()
for(target in unique(example$target)){
oneoption[[target]] <- list()
for(mech in unique(example$mechan)){
oneoption[[target]][[mech]] <- as.character(example$chems[ example$target==target & example$mechan==mech ])
}
}
I am just wondering if there is a more clever way to do it. I tried playing around with lapply
and did not make any progress.
我只是想知道是否有更聪明的方法来做到这一点。我试着玩弄lapply
,但没有取得任何进展。
回答by Se?or O
Using split
:
使用split
:
split(refchem, list(refchem$target, refchem$mechanism))
Should do the trick.
应该做的伎俩。
The new way to access would be refchem$AR.Agonist
新的访问方式是 refchem$AR.Agonist
回答by Frank
If you make a keyed data.table instead, ...
如果您改为使用键控 data.table,...
- you'll still have all the data in one data.frame (instead of a possibly-nested list of many);
- you may find iterating over these subsets nicer; and
- the syntax is pretty clean:
- 您仍然将所有数据保存在一个 data.frame 中(而不是可能嵌套的多个列表);
- 你可能会发现迭代这些子集更好;和
- 语法很干净:
To access a subset:
要访问子集:
DT[.('AR','Agonist')]
To do something for each group, that will be rbind
ed together in the result:
为每个组做一些事情,这将rbind
在结果中一起编辑:
DT[,{do stuff},by=key(DT)]
Similar to aggregate()
, any list of vectors of the correct length can go into the by
, not just the key.
与 类似aggregate()
,任何长度正确的向量列表都可以进入by
,而不仅仅是键。
Finally, DT
came from...
终于,DT
来自...
require(data.table)
DT <- data.table(refchem,key=c('target','mechanism'))
回答by mengeln
You can also use a plyr
function:
您还可以使用一个plyr
函数:
library(plyr)
dlply(example, .(target, mechan))
It has the added advantage of using a function to process the data, if needed (there's an implicit identity
in the above).
如果需要,它具有使用函数来处理数据的额外优势(上面有一个隐含identity
的含义)。