4

假设我有以下列表:

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
4

2 回答 2

8
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 
## 
于 2013-03-12T03:02:52.650 回答
4

unlist先用

table(unlist(test))
于 2013-03-12T03:01:40.940 回答