如何通过id将数据帧转换为R中的列表? [英] how to convert dataframe to list in R by id?
问题描述
基本上我有一个数据集,如下所示:
id <-c(1,1,1,2,2,3,3,3,3)
label< -c 'a','b','c','b','d','a','c','d','e')
mydata< -as.data.frame(cbind (id,label))
mydata $ id <-as.integer(as.character(mydata $ id))
mydata $ label< -as.character(mydata $ label)
mydata
id label
1 1 a
2 1 b
3 1 c
4 2 b
5 2 d
6 3 a
7 3 c
8 3 d
9 3 e
我想将mydata变成mylist看起来像下面这样:
mylist< -list()
mylist [[1] ]< -c('a','b','c')
mylist [[2]]< -c('b','d')
mylist [[3] ]< -c('a','c','d','e')
mylist
[[1]]
[1]ab c
[[2]]
[1]bd
[[3]]
[1]acde
那么,我怎么去mydata去mylist?
注意:我的实际数据框有200万行。
[背景:我正在研究一个多标签分类问题,需要计算F1,精度和回忆,而不是。标签是可变的,我以为我可以把所有的这些都纳入一个大的列表,并做比较。]
看看在?split
:
split(mydata $ label,mydata $ id )
#$`1`
#[1]abc
#
#$`2`
#[1] bd
#
#$`3`
#[1]acde
I've been struggling whole evening to figure out how to do this in R.
Basically I have a dataset like the following:
id<-c(1,1,1,2,2,3,3,3,3)
label<-c('a', 'b', 'c', 'b', 'd', 'a', 'c', 'd', 'e')
mydata<-as.data.frame(cbind(id, label))
mydata$id<-as.integer(as.character(mydata$id))
mydata$label<-as.character(mydata$label)
mydata
id label
1 1 a
2 1 b
3 1 c
4 2 b
5 2 d
6 3 a
7 3 c
8 3 d
9 3 e
I want to transform mydata into mylist to look like the following:
mylist<-list()
mylist[[1]]<-c('a', 'b', 'c')
mylist[[2]]<-c( 'b', 'd')
mylist[[3]]<-c( 'a', 'c', 'd', 'e')
mylist
[[1]]
[1] "a" "b" "c"
[[2]]
[1] "b" "d"
[[3]]
[1] "a" "c" "d" "e"
So, how do I go form mydata to mylist?
NOTE: my actual dataframe has ~2 million rows.
[background: I'm working on a multi-label classification problem and would need to calculate F1, precision and recall and as the no. labels are variable I thought I could chuck all of them into a big list and do the comparison that way]
Have a look at ?split
:
split(mydata$label, mydata$id)
#$`1`
#[1] "a" "b" "c"
#
#$`2`
#[1] "b" "d"
#
#$`3`
#[1] "a" "c" "d" "e"
这篇关于如何通过id将数据帧转换为R中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!