How to group a vector into a list of vectors?

后端 未结 4 2202
借酒劲吻你
借酒劲吻你 2020-12-06 19:13

I have some data which looks like this (fake data for example\'s sake):

dressId        color 
6              yellow 
9              red
10             green         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 19:29

    split.data.frame is a good way to organize this; then extract the color component.

    d <- data.frame(dressId=c(6,9,10,10,10,12,12),
                   color=factor(c("yellow","red","green",
                                  "purple","yellow",
                                  "purple","red"),
                     levels=c("red","orange","yellow",
                              "green","blue","purple")))
    

    I think the version you want is actually this:

    ss <- split.data.frame(d,d$dressId)
    

    You can get something more like the list you requested by extracting the color component:

    lapply(ss,"[[","color")
    

提交回复
热议问题