Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get only the most often occurring values from a list of vectors

I have data as follows:

dat <- list(nr1 = list(list_of_account_numbers = " 0000000000", 
    " NL11BANKO0111111111", " NL11BANKO0111111111", " NL11BANKO0111111111", 
    " NL11BANKO0111111111", " NL11BANKO0111111111", " NL11BANKO0111111113", 
    " NL11BANKO0111111111", " NL11BANKO0111111112", " NL11BANKO0111111113", 
    " NL11BANKO0111111111", " NL11BANKO0111111112", " NL11BANKO0111111113", 
    " NL11BANKO0111111111", " NL11BANKO0111111111", " 0000000000", 
    " 0000000000"), nr2 = list(list_of_account_numbers = " NL30ABNA0111111111", 
    " NL31RABO0111111111", " NL30ABNA0111111111", " NL30ABNA0111111111", 
    " NL30ABNA0111111111", " NL31RABO0111111111", " NL31RABO0111111111", 
    " NL52RABO0111111111", " NL74INGB0111111111", " NL74INGB0111111111", 
    " NL30ABNA0111111111", " NL30ABNA0111111111", " NL30ABNA0111111111", 
    " NL74INGB0111111111", " NL74INGB0111111111", " NL74INGB0111111111", 
    " NL74INGB0111111111", " NL74INGB0111111111", " NL74INGB0111111111", 
    " NL16DEUT0111111111"), nr3 = list(
        list_of_account_numbers = " NL11BANKO0111111111", " NL11BANKO0111111111", 
        " NL11BANKO0111111111", " NL11BANKO0111111111", " NL11BANKO0111111113", 
        " NL11BANKO0111111111", " NL11BANKO0111111111", " NL11BANKO0111111113", 
        " NL11BANKO0111111111", " NL11BANKO0111111111", " NL11BANKO0111111113", 
        " NL11BANKO0111111111", " NL11BANKO0111111111"))

I am trying to write a code that for each list item (nr1,nr2,nr3), get the top 3 most occurring values. There are two additional issues.

  1. Some list items have the value 0000000000, which should be excluded.
  2. Some list items do not have 3 values, but only one or two.

I thought the first thing to do is to unlist the items and to remove the occurrences of 0000000000;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

IBAN_numbers <- list()
y <- " 0000000000"
for (i in 1:length(dat)) { 
  IBAN_numbers[[i]] <- unlist(dat[i])
  IBAN_numbers[[i]] = IBAN_numbers[[i]][! IBAN_numbers[[i]] %in% y]
} 

But I am not sure how achieve the last point.

table(IBAN_numbers[[1]])

#  NL11BANKO0111111111  NL11BANKO0111111112  NL11BANKO0111111113 
#                    9                    2                    3 

table(IBAN_numbers[[2]])
    #  NL16DEUT0111111111  NL30ABNA0111111111  NL31RABO0111111111  NL52RABO0111111111  NL74INGB0111111111 
#                   1                   7                   3                   1                   8 

table(IBAN_numbers[[3]])
    #  NL11BANKO0111111111  NL11BANKO0111111113 
#                   10                    3 

I could do something like:

IBAN_numbers <- list()
y <- " 0000000000"
for (i in 1:length(dat)) { 
  IBAN_numbers[[i]] <- unlist(dat[i])
  IBAN_numbers[[i]] = IBAN_numbers[[i]][! IBAN_numbers[[i]] %in% y]
  IBAN_numbers[[i]] = table(IBAN_numbers[[i]])
} 

So for the middle table, I would want only three entries (I do not care which option with one occurence it takes, as long as it does not crash).

Could anyone help me with the last step?

>Solution :

You may do this with lapply

y <- " 0000000000"
lapply(dat, function(x) {
  x <- unlist(x)
  head(sort(table(x[x != y]), decreasing = TRUE), 3)
})

#$nr1

#NL11BANKO0111111111  NL11BANKO0111111113  NL11BANKO0111111112 
#                  9                    3                    2 

#$nr2

# NL74INGB0111111111  NL30ABNA0111111111  NL31RABO0111111111 
#                  8                   7                   3 

#$nr3

# NL11BANKO0111111111  NL11BANKO0111111113 
#                  10                    3 

You may use names(head(sort(table(x[x != y]), decreasing = TRUE), 3)) if you are interested only in names.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading