Convert data frame to list

空扰寡人 提交于 2019-11-27 16:17:43

Using split:

split(refchem, list(refchem$target, refchem$mechanism))

Should do the trick.

The new way to access would be refchem$AR.Agonist

If you make a keyed data.table instead, ...

  • 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:

To access a subset:

DT[.('AR','Agonist')] 

To do something for each group, that will be rbinded together in the result:

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.

Finally, DT came from...

 require(data.table)
 DT <- data.table(refchem,key=c('target','mechanism'))

You can also use a plyr function:

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!