簡體   English   中英

如何獲取具有不同長度的列表中項目的頻率

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM