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!
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")
)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With