gpt4 book ai didi

r - 如何计算列表中唯一向量的数量?

转载 作者:行者123 更新时间:2023-12-04 13:48:02 24 4
gpt4 key购买 nike

假设我有一个向量列表。我想要该列表中唯一向量的列表及其频率。我可以通过 unique 获得唯一值的列表,但我不知道如何获得计数向量。

my.list <- list(c(1, 1, 0), c(1, 1, 0))
> unique(my.list) # gives correct answer
# [[1]]
# [1] 1 1 0

现在我想要一些东西,它可以为我提供 unique(my.list) 的每个元素的次数向量。被重复了。在这种情况下,它应该是一个带有 2 元素的向量.

使用 table不起作用,因为它分别获取向量的每个元素(0 和 1 值):
> table(my.list)
# my.list.2
# my.list.1 0 1
# 0 1 0
# 1 0 2

有任何想法吗?我宁愿不 paste如果可以的话,将它们转换为一个字符串,然后将它们重新分隔为向量。

最佳答案

使用 match在整个列表与唯一列表中:

my.list <- list(c(1, 1, 0), c(1, 1, 0), c(2, 1, 0))
table(match(my.list,unique(my.list)))

#1 2
#2 1

cbind(
data.frame(id=I(unique(my.list))),
count=as.vector(table(match(my.list,unique(my.list))))
)
# id count
#1 1, 1, 0 2
#2 2, 1, 0 1

关于r - 如何计算列表中唯一向量的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36047743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com