How do I group vectors into vector lists?

I have some data that looks like this (such as false data):

dressId        color 
6              yellow 
9              red
10             green 
10             purple 
10             yellow 
12             purple 
12             red

Where color is a factor vector There is no guarantee that all possible levels of factors actually appear in the data (for example, the color "blue" can also be one of them)

I need a vector list to group the available colors for each garment:

[[1]]
yellow  

[[2]] 
red    

[[3]] 
green purple yellow 

[[4]] 
purple red

It would be nice to keep the ID of the dress (for example, the list is the second column, and the ID is the data frame of the first column), but it is not necessary

I wrote a loop that traverses the data frame rows of the row, and the next ID is the same. It adds color to the vector I'm sure the data is sorted by ID When the ID in the first column changes, it adds the vector to the list:

result <- NULL 
while(blah blah) 
{
    some code which creates the vector called "colors" 
    result[[dressCounter]] <- colors 
    dressCounter <- dressCounter + 1
}

After trying to get all the necessary count variables, I found that I was not happy that it didn't work For the first time, the color is

[1] yellow
Levels: green yellow purple red blue

And it is cast to an integer, so the result becomes 2

In the second cycle repetition, the color contains only red, and the result becomes a simple integer vector, [1] 2.4

In the third iteration, the color is now a vector,

[1] green  purple yellow
Levels: green yellow purple red blue

Oh, I see

result[[3]] <- colors

What on earth did I do wrong? Is there a way to initialize the result, so it will not be converted to a digital vector, but become a vector list?

Also, is there another way to complete the whole thing instead of "rolling my own"?

Solution

split. data. Frame is a good way to organize this; Then extract the color components

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

I think the version you want is actually like this:

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

You can extract the color component to get a list more like your request:

lapply(ss,"[[","color")
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>