Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Count frequency of values in nested list with sub-elements

I have a nested list that contains country names. I want to count the frequency of the countries, whereby +1 is added with each mention in a sub-list (regardless of how often the country is mentioned in that sub-list).

For instance, if I have this list:

[[1]]
[1] "Austria" "Austria" "Austria"

[[2]]
[1] "Austria" "Sweden"

[[3]]
[1] "Austria" "Austria" "Sweden"  "Sweden" "Sweden" "Sweden"

[[4]]
[1] "Austria" "Austria" "Austria"

[[5]]
[1] "Austria" "Japan" 

... then I would like the result to be like this:

country        freq
====================
Austria         5
Sweden          2
Japan           1

I have tried various ways with lapply, unlist, table, etc. but nothing worked the way I would need it. I would appreciate your help!

like image 604
anpami Avatar asked Oct 27 '20 08:10

anpami


2 Answers

One way with lapply(), unlist() and table():

count <- table(unlist(lapply(lst, unique)))
count
# Austria   Japan  Sweden 
#       5       1       2 


as.data.frame(count)
#      Var1 Freq
# 1 Austria    5
# 2   Japan    1
# 3  Sweden    2

Reproducible data (please provide yourself next time):

lst <- list(
  c('Austria', 'Austria', 'Austria'), 
  c("Austria", "Sweden"), 
  c("Austria", "Austria", "Sweden", "Sweden", "Sweden", "Sweden"), 
  c("Austria", "Austria", "Austria"), 
  c("Austria", "Japan")
)
like image 129
sindri_baldur Avatar answered Sep 28 '22 07:09

sindri_baldur


Here is another base R option

colSums(
  do.call(
    rbind,
    lapply(
      lst,
      function(x) table(factor(x, levels = unique(unlist(lst)))) > 0
    )
  )
)

which gives

Austria  Sweden   Japan
      5       2       1
like image 30
ThomasIsCoding Avatar answered Sep 28 '22 08:09

ThomasIsCoding