Reputation: 111
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?
Upvotes: 0
Views: 416
Reputation: 23129
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
Upvotes: 1