Reputation: 375
I have a list of numeric vectors. Some of these vectors are identical, and so what I want to do is find a simple and quick way to group the identical vectors together and create a list for each of the groups of identical vectors, and potentially save all of the lists we just created into a single giant list. Any help would be appreciated!
Upvotes: 2
Views: 350
Reputation: 66819
Here's an example:
x <- list(v1= 1:3, v2 = 2:3, v3 = 2:3, v4 = 1:3, v5 = 4)
You can assign each element of the list a "group ID" using match
and unique
:
ux <- unique(x)
# str(ux)
# List of 3
# $ : int [1:3] 1 2 3
# $ : int [1:2] 2 3
# $ : num 4
gid <- match(x,ux)
# [1] 1 2 2 1 3
The gid
corresponds to an element of ux
.
Whatever you want to do from there is pretty straightforward, like ave(x, gid, FUN=some_function)
.
Upvotes: 2