如何获得具有不同长度的列表中项目的频率 [英] How to get the frequency of items in a list with the different length

查看:38
本文介绍了如何获得具有不同长度的列表中项目的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下列表:

test<-list(c("a","b","c"),c("a"),c("c"))
>test
[[1]]
[1] "a" "b" "c"

[[2]]
[1] "a"

[[3]]
[1] "c"

我该怎么做(或要使用的功能)来获得像这样的列表中唯一项目的出现频率??

What do I do(or functions to use) to get the frequency of unique items in a list like this:?

a 2
b 1
c 2

我尝试使用table(test),但出现以下错误

I tried using table(test), but I get the following error

> 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 
## 

这篇关于如何获得具有不同长度的列表中项目的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆