How to sum columns row-wise in R quickly

Imagine I want to calculate the percentage of the vote that Kamela Harris got in Albany County using this data frame. I want to add up all the Kamela Harris columns and divide by the total votes in the Presidential race. There is a quick trick to do this with R using the tidyverse and purr library.

  mutate(
    harris_percent =
      reduce(select(., contains('kamala_d_harris')), `+`)/
      reduce(select(., contains('electors_for_president_and_vice_president')), `+`)
    )

The above code uses the reduce command against a subset of the dataframe containing the Kamala Harris columns which reduces them to one column in a vectorized format using the addition operator. You can use any vectorized operation or function rather then `+` there.

Here are column names for reference:

[1] "town"                                                             "ward"                                                            
[3] "ed" "ballots_cast_total_ballots_cast"
[5] "ballots_cast_blank_ballots_cast2" "electors_for_president_and_vice_president_dem_kamala_d_harris"
[7] "electors_for_president_and_vice_president_rep_donald_j_trump" "electors_for_president_and_vice_president_con_donald_j_trump"
[9] "electors_for_president_and_vice_president_wfp_kamala_d_harris" "electors_for_president_and_vice_president_write_in"
[11] "electors_for_president_and_vice_president_over_votes" "electors_for_president_and_vice_president_under_votes"
[13] "united_states_senator_dem_kirsten_e_gillibrand" "united_states_senator_rep_michael_d_sapraicone"
[15] "united_states_senator_con_michael_d_sapraicone" "united_states_senator_wfp_kirsten_e_gillibrand"
[17] "united_states_senator_lar_diane_sare" "united_states_senator_write_in3"
[19] "united_states_senator_over_votes4" "united_states_senator_under_votes5"
[21] "representative_in_congress_20th_district_dem_paul_d_tonko" "representative_in_congress_20th_district_rep_kevin_m_waltz"
[23] "representative_in_congress_20th_district_con_kevin_m_waltz" "representative_in_congress_20th_district_wfp_paul_d_tonko"
[25] "representative_in_congress_20th_district_write_in6" "representative_in_congress_20th_district_over_votes7"
[27] "representative_in_congress_20th_district_under_votes8" "state_senator_43rd_district_dem_alvin_gamble"
[29] "state_senator_43rd_district_rep_jake_ashby" "state_senator_43rd_district_con_jake_ashby"
[31] "state_senator_43rd_district_write_in9" "state_senator_43rd_district_over_votes10"
[33] "state_senator_43rd_district_under_votes11" "state_senator_46th_district_dem_patricia_a_fahy"
[35] "state_senator_46th_district_rep_ted_danz_jr" "state_senator_46th_district_con_ted_danz_jr"
[37] "state_senator_46th_district_wfp_patricia_a_fahy" "state_senator_46th_district_write_in12"
[39] "state_senator_46th_district_over_votes13" "state_senator_46th_district_under_votes14"
[41] "member_of_assembly_102nd_district_dem_janet_s_tweed" "member_of_assembly_102nd_district_rep_christopher_tague"
[43] "member_of_assembly_102nd_district_con_christopher_tague" "member_of_assembly_102nd_district_wfp_janet_s_tweed"
[45] "member_of_assembly_102nd_district_write_in15" "member_of_assembly_102nd_district_over_votes16"
[47] "member_of_assembly_102nd_district_under_votes17" "member_of_assembly_107th_district_dem_chloe_e_pierce"
[49] "member_of_assembly_107th_district_rep_scott_h_bendett" "member_of_assembly_107th_district_con_scott_h_bendett"
[51] "member_of_assembly_107th_district_write_in18" "member_of_assembly_107th_district_over_votes19"
[53] "member_of_assembly_107th_district_under_votes20" "member_of_assembly_108th_district_john_t_mc_donald_iii"
[55] "member_of_assembly_108th_district_write_in21" "member_of_assembly_108th_district_over_votes22"
[57] "member_of_assembly_108th_district_under_votes23" "member_of_assembly_109th_district_dem_gabriella_a_romero"
[59] "member_of_assembly_109th_district_rep_alicia_m_purdy" "member_of_assembly_109th_district_con_alicia_m_purdy"
[61] "member_of_assembly_109th_district_wfp_gabriella_a_romero" "member_of_assembly_109th_district_write_in24"
[63] "member_of_assembly_109th_district_over_votes25" "member_of_assembly_109th_district_under_votes26"
[65] "member_of_assembly_110th_district_dem_phillip_g_steck" "member_of_assembly_110th_district_rep_jeff_madden"
[67] "member_of_assembly_110th_district_con_jeff_madden" "member_of_assembly_110th_district_wfp_phillip_g_steck"
[69] "member_of_assembly_110th_district_write_in27" "member_of_assembly_110th_district_over_votes28"
[71] "member_of_assembly_110th_district_under_votes29" "district_attorney_dem_lee_c_kindlon"
[73] "district_attorney_rep_ralph_f_ambrosio" "district_attorney_wfp_lee_c_kindlon"
[75] "district_attorney_write_in30" "district_attorney_over_votes31"
[77] "district_attorney_under_votes32" "surrogate_court_judge_dem_deborah_s_kearns"
[79] "surrogate_court_judge_wfp_deborah_s_kearns" "surrogate_court_judge_write_in33"
[81] "surrogate_court_judge_over_votes34" "surrogate_court_judge_under_votes35"
[83] "family_court_judge_dem_jaime_lynn_czajka" "family_court_judge_dem_jillian_e_faison"
[85] "family_court_judge_dem_ricja_d_rice_ghyll" "family_court_judge_wfp_jasper_mills_iii"
[87] "family_court_judge_wfp_jillian_e_faison" "family_court_judge_wfp_ricja_d_rice_ghyll"
[89] "family_court_judge_write_in36" "family_court_judge_over_votes37"
[91] "family_court_judge_under_votes38" "county_legislator_county_legislative_district_5_dem_susan_m_pedo"
[93] "county_legislator_county_legislative_district_5_write_in39" "county_legislator_county_legislative_district_5_over_votes40"
[95] "county_legislator_county_legislative_district_5_under_votes41" "cohoes_city_council_member_ward_6_dem_april_m_trudeau"
[97] "cohoes_city_council_member_ward_6_con_justin_m_alesio" "cohoes_city_council_member_ward_6_vfa_april_m_trudeau"
[99] "cohoes_city_council_member_ward_6_write_in42" "cohoes_city_council_member_ward_6_over_votes43"
[101] "cohoes_city_council_member_ward_6_under_votes44" "town_justice_town_of_bethlehem_dem_erin_r_lynch"
[103] "town_justice_town_of_bethlehem_write_in45" "town_justice_town_of_bethlehem_over_votes46"
[105] "town_justice_town_of_bethlehem_under_votes47" "village_trustee_village_of_green_island_dem_michele_a_heffern"
[107] "village_trustee_village_of_green_island_dem_richard_r_jones_jr" "village_trustee_village_of_green_island_dem_joseph_a_liotta_sr"
[109] "village_trustee_village_of_green_island_write_in48" "village_trustee_village_of_green_island_over_votes49"
[111] "village_trustee_village_of_green_island_under_votes50" "village_trustee_village_of_green_island_dem_barbara_belokopitsky"
[113] "village_trustee_village_of_green_island_write_in51" "village_trustee_village_of_green_island_over_votes52"
[115] "village_trustee_village_of_green_island_under_votes53" "highway_superintendent_town_of_westerlo_dem_james_e_brush"
[117] "highway_superintendent_town_of_westerlo_rep_david_pecylak" "highway_superintendent_town_of_westerlo_con_david_pecylak"
[119] "highway_superintendent_town_of_westerlo_write_in54" "highway_superintendent_town_of_westerlo_over_votes55"
[121] "highway_superintendent_town_of_westerlo_under_votes56" "proposal_one_an_amendment_yes"
[123] "proposal_one_an_amendment_no" "proposal_one_an_amendment_over_votes57"
[125] "proposal_one_an_amendment_under_votes58" "proposal_two_town_of_bethlehem_yes59"
[127] "proposal_two_town_of_bethlehem_no60" "proposal_two_town_of_bethlehem_over_votes61"
[129] "proposal_two_town_of_bethlehem_under_votes62"

Leave a Reply

Your email address will not be published. Required fields are marked *