[英]How to get the frequency of items in a list with the different length
假设我有以下列表:
test<-list(c("a","b","c"),c("a"),c("c"))
>test
[[1]]
[1] "a" "b" "c"
[[2]]
[1] "a"
[[3]]
[1] "c"
我该怎么做(或使用函数)来获取列表中的唯一项目的频率,如下所示:?
a 2
b 1
c 2
我尝试使用表(测试),但我得到以下错误
> table(test)
Error in table(test) : all arguments must have the same length
test <- list(c("a", "b", "c"), c("a"), c("c"))
# If you want count accross all elements
table(unlist(test))
##
## a b c
## 2 1 2
# If you want seperate counts in each item of list
lapply(test, table)
## [[1]]
##
## a b c
## 1 1 1
##
## [[2]]
##
## a
## 1
##
## [[3]]
##
## c
## 1
##
首先使用unlist
table(unlist(test))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.