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.
说我有一个向量列表。我想要列出该列表中的唯一向量及其频率。我可以获得唯一的唯一值列表,但我无法弄清楚如何获得计数的向量。
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. In this case, that should be a vector with the element 2
.
现在我想要一些东西,它给出了每个独特元素(my.list)重复次数的向量。在这种情况下,它应该是具有元素2的向量。
Using table
doesn't work, because it takes each of the elements of the vector (the 0 and 1 values) separately:
使用表不起作用,因为它分别取向量的每个元素(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.
有任何想法吗?我宁愿不将它们粘贴到一个字符串中,然后如果我可以帮助它们将它们重新分成矢量。
2 个解决方案
#1
7
Use match
on the entire list vs. the unique list:
在整个列表中使用匹配与唯一列表:
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
#2
4
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_by和summarize(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
#1
7
Use match
on the entire list vs. the unique list:
在整个列表中使用匹配与唯一列表:
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
#2
4
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_by和summarize(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