user3642360
user3642360

Reputation: 792

How to find out frequency of each unique elements in a list in R

I have a list of length 927. Length of each element in the list is unequal. (Please condiser the following example. I have given only first 6 elements of the list)

[[1]]
[1] "DIR" "EMERGING" "UNP"      

[[2]]
[1] "DIR" "ECO" "UNP"             

[[3]]
[1] "DIRECT"

[[4]]
[1] "DIR" "ECO" "NATURAL"              

[[5]]
[1] "DIR"

[[6]]
[1] "DIR"

My goal is to find out unique elements in the list and count the frequency for each unique elements in the list. Using unique(list_name), I have found out the unique elements in the list. But I am unable to find out the frequency for each unique list element. Any help would be appreciable. Thanks in advance!

Upvotes: 0

Views: 645

Answers (1)

David Arenburg
David Arenburg

Reputation: 92292

Could do (assuming your list called list_name)

table(unlist(lapply(list_name, paste, collapse = " ")))

## DIR  DIR ECO NATURAL      DIR ECO UNP DIR EMERGING UNP           DIRECT 
##   2                1                1                1                1 

Upvotes: 4

Related Questions