假设我有以下列表:
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),但是我收到以下错误
> table(test)
Error in table(test) : all arguments must have the same length
答案 0 :(得分:7)
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
##
答案 1 :(得分:4)
先使用unlist
table(unlist(test))