简体   繁体   English

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

[英]How do I count the number of unique vectors in a list?

Say I have a list of vectors. 说我有一个向量列表。 I want a list of the unique vectors in that list, and their frequencies. 我想要列出该列表中的唯一向量及其频率。 I can get a list of the unique values with unique , but I can't figure out how to get a vector of the counts. 我能得到的唯一值的列表与unique ,但我无法弄清楚如何获得数的向量。

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

Now I want something that gives me a vector of the number of times each element of unique(my.list) was repeated. 现在我想要的东西给了我一个重复的每个元素unique(my.list)重复次数的向量。 In this case, that should be a vector with the element 2 . 在这种情况下,它应该是具有元素2的向量。

Using table doesn't work, because it takes each of the elements of the vector (the 0 and 1 values) separately: 使用table不起作用,因为它分别取向量的每个元素(0和1值):

> table(my.list)
#          my.list.2
# my.list.1 0 1
#         0 1 0
#         1 0 2

Any ideas? 有任何想法吗? I would rather not paste these into a string and then re-separate them into vectors if I can help it. 我宁愿不将它们paste到一个字符串中,然后如果我可以帮助它,则将它们重新分成矢量。

Use match on the entire list vs. the unique list: 在整个列表中使用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

One approach, maybe more complicated than it needs to be: 一种方法,可能比它需要的更复杂:

library(dplyr)
df <- do.call(rbind, my.list) %>% as.data.frame()
df %>% group_by_(.dots = names(df)) %>% summarise(count = n())

# Source: local data frame [1 x 4]
# Groups: V1, V2 [?]
# 
#      V1    V2    V3 count
#   (dbl) (dbl) (dbl) (int)
# 1     1     1     0     2

Edit: 编辑:

Per the comment below by @docendodiscimus, group_by and summarise(n()) is equivalent to count_ : 根据@docendodiscimus下面的评论, group_bysummarise(n())相当于count_

df %>% count_(names(df))    # or just count_(df, names(df))

# Source: local data frame [1 x 4]
# Groups: V1, V2 [?]
# 
#      V1    V2    V3     n
#   (dbl) (dbl) (dbl) (int)
# 1     1     1     0     2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R ::我有一个向量列表,如何计算两个向量在所有组合中共有的元素数量? - R:: I have a list of vectors, how do I count the number of elements that two vectors have in common for all combinations? 如何为一个向量中的每个唯一值生成一个数据帧,显示两个向量之间唯一对的数量? - How do I generate a dataframe displaying the number of unique pairs between two vectors, for each unique value in one of the vectors? 如何计算数据子集中唯一字符向量的数量 - How to count number of unique character vectors within a subset of data 如何转置列表的每个向量,给它们分配一个唯一的数字,并将其绑定到r中? - How can I transpose each vectors of a list assign a unique number to them, and bind them in r? 如何计算两个向量之间具有相同位置的匹配数? - How do I count the number of matches that have the same position between two vectors? 如何在Rcpp中创建向量列表? - How do I create a list of vectors in Rcpp? 如何计算读入此列表的文件数? - How do I count the number of files read into this list? R/dplyr:如何在不重复计算的情况下计算一次观察的唯一出现次数? - R/dplyr: How do I count the number of unique occurrences of an observation over time without double counting? 如何计算R单日最大唯一条目数? - How do I count the largest number of unique entries in a single day in R? 如何计算列表的每个元素中的唯一字符串数 - How to count the number of unique strings in each element of a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM