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

R – Number of times an item from one list appears in another list

I have two lists of patient ID’s. One is short, the other is very long.

I’d like to know how many times patients from the short list appear in the long list, preferably using dplyr.

library(tidyr)
library(tidyverse)

shortlist<-tribble(
  ~PatientID, 
  #--
  10,
  11,
  12,
  13,
  14,
  15
)

longlist<-tribble(
  ~PatientID,
  #--
  10,
  10,
  11,
  12,
  12,
  12,
  13,
  14,
  15,
  15,
  15,
  16,
  17
)

Desired output would be something like:

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

10 2

11 1

12 3

13 1

and so on

>Solution :

We filter the ‘PatientID’ based on the ‘shortlist’ and get the count

library(dplyr)
longlist %>% 
  filter(PatientID %in% shortlist$PatientID) %>% 
  count(PatientID)

-output

# A tibble: 6 × 2
  PatientID     n
      <dbl> <int>
1        10     2
2        11     1
3        12     3
4        13     1
5        14     1
6        15     3
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