[英]R: list of vectors - grouping identical vectors together
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!
任何帮助,将不胜感激!
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
:您可以使用
match
和unique
为列表的每个元素分配一个“组 ID”:
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
. gid
对应于ux
一个元素。
Whatever you want to do from there is pretty straightforward, like ave(x, gid, FUN=some_function)
.无论您想从那里做什么都非常简单,例如
ave(x, gid, FUN=some_function)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.