Home > Blockchain >  Filter rows that contain values from a list
Filter rows that contain values from a list

Time:05-06

I'm using RStudio, and I have one dataframe containing a list of candidates, and a dataframe containing all votes of each voting location, and by candidate.

I want to extract only the rows containing the votes of the candidates of this list.

Example:

  • List:

     Candidate
     A
     B
     D
     G
    
  • Votes:

     Candidate       Number of Votes
     A               124
     B               52
     C               13
     D               62
     E               33
     F               7
     G               67
    

I want then to create a new dataframe containing only the candidates and votes of the "List":

  • Votes of listed candidates:

     Candidate       Number of Votes
     A               124
     B               52
     D               62
     G               67
    

The example is a simplification. My database contains over 30.000 "candidates"

Thanks in advance

CodePudding user response:

We can use subset in base R

subset(Votes, Candidate %in% List$Candidate)

CodePudding user response:

You can merge both data frames with merge():

merge(df1, df2, by = "Candidate", all.x = TRUE)

or equivalently

dplyr::left_join(df1, df2, by = "Candidate")

#   Candidate Number_of_Votes
# 1         A             124
# 2         B              52
# 3         D              62
# 4         G              67

Data
df1 <- structure(list(Candidate = c("A", "B", "D", "G")), class = "data.frame", row.names = c(NA, -4L))
df2 <- structure(list(Candidate = c("A", "B", "C", "D", "E", "F", "G"),
Number_of_Votes = c(124L, 52L, 13L, 62L, 33L, 7L, 67L)), class = "data.frame", row.names = c(NA, -7L))
  • Related