[R] Frequencies for a list of vectors

William Dunlap wdunlap at tibco.com
Tue Aug 5 22:51:02 CEST 2014


You can those vectors into character strings and pass them to table().  E.g.,

> d <- list(`30008`=c(1,0,1,0), `60008`=c(0,0,1,0), `90008`=c(0,0,1,0), `100007`=1, `130001`=c(0,1), `130007`=c(1,0,1,0))
> dChar <- vapply(d, FUN=function(di)paste(di, collapse=" "), FUN.VALUE="")
> dTable <- table(dChar)
> dTable
dChar
0 0 1 0     0 1       1 1 0 1 0
      2       1       1       2
> dTable["1 0 1 0"]
1 0 1 0
      2

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Aug 5, 2014 at 10:39 AM, Marie-Pierre Sylvestre
<mp.sylvestre at gmail.com> wrote:
> Dear R users,
>
> I have a list of vectors (list is called HTNlist). Each vector is of length
> 1 to 4 and takes only 0 and 1 as values. E.g.
>
> head(HTNlist)
> $`30008`
> [1] 1 0 1 0
>
> $`60008`
> [1] 0 0 1 0
>
> $`90008`
> [1] 0 0 1 0
>
> $`100007`
> [1] 1
>
> $`130001`
> [1] 0 1
>
> $`130007`
> [1] 1 0 1 0
>
> I would like to obtain a frequency table for the elements of the list. I
> want to know how many of
> '1 0 0' I have in the list, how many '1 0 1 0' etc.
>
> Can you please help?
>
> Thank you in advance,
> MP
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list