Q:

Subset data by columns with equivalent values in R

I'm running into a problem of how to subset my data based on a condition in which a row's observation of a column variable is equal to a different column variable's observation in that same row.
The example I'm working with here is votes in an election
library(dplyr)
library(tidyverse)
library(ggplot2)
library(matrixStats)

Candidate1Votes <- c(45, 18, 34)
Candidate2Votes <- c(43, 52, 33)
Candidate3Votes <- c(12, 30, 33)
Precinct <- c(1, 2, 3)
election.matrix <- cbind(Precinct, Candidate1Votes, Candidate2Votes,
                           Candidate3Votes)

This code gets a matrix like this:
Precinct    Candidate1    Candidate2    Candidate3 
1           45             43             12
2           18             52             30
3           34             33             33

I want to know which candidate won the greatest number of votes in each precinct (and so I generate row-wise maximums):
PrecinctWinners <- rowMaxs(election.matrix)

I bind it to the matrix so that it now looks something like this:
Precinct    Candidate1    Candidate2    Candidate3    PrecinctWinners
1           45             43             12             45
2           18             52             30             52
3           34             33             33             34

I've converted this to a data frame for further use:
election.df <- as.data.frame(election.matrix)

Here's my problem. I want to subset the data for rows in which Candidate 1 took a plurality of the votes (where election.df$Candidate1 = election.df$PrecinctWinners). How do you create a subsetting condition that selects rows in the data based on two different observations in those rows having the same value?

A:

As an alternative to the solution in the comment by Duck, you can use dplyr as follows:
library(dplyr)

election.df <- tibble(precint = c(1, 2, 3),
                      Candiate1 = c(45, 18, 34),
                      Candiate2 = c(43, 52, 33),
                      Candiate3 = c(12, 30, 33),
                      PrecintWinners = c(45, 52, 34))

election.df %>%
  filter(Candiate1 == PrecintWinners)

#   precint Candiate1 Candiate2 Candiate3 PrecintWinners
#     <dbl>     <dbl>     <dbl>     <dbl>          <dbl>
# 1       1        45        43        12             45
# 2       3        34        33        33             34

