简体   繁体   中英

Summarize the list of the table in R

I have a list of data which I have represented here with dat. I am trying to summarize the data in a way that gives me the number of element in each list, along with unique elements and what's the frequency of each element (although in the example file there's 4 element, it can be much larger in the real file).

dat = list(c("a","b","c","d"),
           c("a","a"),
           c("b"),
           c("c","a","c"))

Num_element = sapply(dat, length)
Num_table = sapply(dat, table)
Num_unique = sapply(Num_table, length)

I am looking to get something of sort (which I know is wrong in many ways)

Summary_dat = cbind.data.frame(Num_element,Num_unique, Num_table)

Basically what I envision could be a nested dataframe, such that the output may look like:

  Num_element Num_unique  Num_table
1           4          4 a b c d 
2           2          1 1 1 1 1(in the same line as above)
3           1          1
4           3          2

I am not sure if it's possible to do what I am asking. If not, what will be a good way to represent the data, which can then also be exported to excel?

May be something like this is what you want?

data.frame(Num_element = Num_element,
           Num_unique = Num_unique,
           Num_table = sapply(Num_table, function(x) paste(names(x), x, collapse=' ')))

#  Num_element Num_unique       Num_table
#1           4          4 a 1 b 1 c 1 d 1
#2           2          1             a 2
#3           1          1             b 1
#4           3          2         a 1 c 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM