以下是一个解决方案
   
    dplyr
   
   (比已经发布的解决方案更难看,但花了很多时间才发布):
  
  # setting up the data
# **note that I've changed "NA" entries to NA **
Legislator <- c("Allen", "Barber", "Cale", "Devin", "Egan", "Floyd")
Party <- c("R", "D", "D", "R", "R", "R")
vote1 <- c("yes", NA, "no", "no", "yes", "yes")
vote2 <- c("no", "no", NA, "no", "yes", "no")
vote3 <- c(NA, "no", "yes", "no", "yes", "yes")
vote4 <- c("no", "yes", "yes", "yes", NA, "no")
vote5 <- c("yes", "no", "yes", "yes", "no", "yes")
vote6 <- c("yes", "yes", "no", "yes", "no", "no")
vote7 <- c("no", "no", "yes", "yes", "no", "yes")
rollcall <- as.data.frame(base::cbind(Legislator, Party, vote1, vote2, vote3, vote4, vote5, vote6, vote7))
# converting to long format
library(tidyr)
#> Warning: package 'tidyr' was built under R version 3.4.2
rollcall_long <- tidyr::gather(rollcall, vote, response, vote1:vote7, factor_key = TRUE)
# compute frenquency table
library(dplyr)
vote_frequency <- rollcall_long %>% 
  dplyr::filter(!is.na(response)) %>% # remove NAs
  dplyr::group_by(Party, vote, response) %>% # compute frequency by these grouping variables
  dplyr::summarize(counts = n()) %>% # get the count of each response
  dplyr::mutate(perc = counts / sum(counts)) %>% # compute its percentage
  dplyr::arrange(vote, response, Party) %>% # arrange it properly
  dplyr::filter(response == "yes") %>% # select only yes responses ("Ayes")
dplyr::select(-counts, -response)  # remove counts and response variables
# compute Partisanship score
Partisanship_df <- tidyr::spread(vote_frequency, Party, perc)
Partisanship_df[is.na(Partisanship_df)] <- 0 # replacing NA with 0 because NA here represents that not a single "yes" was found
Partisanship_df$Partisanship <- abs(Partisanship_df$D - Partisanship_df$R)
# removing unnecessary columns
Partisanship_df %>% dplyr::select(-c(R, D))
#> # A tibble: 7 x 2
#> # Groups: vote [7]
#>   vote  Partisanship
#> * <fct>        <dbl>
#> 1 vote1        0.750
#> 2 vote2        0.250
#> 3 vote3        0.167
#> 4 vote4        0.667
#> 5 vote5        0.250
#> 6 vote6        0    
#> 7 vote7        0
  
   于2018年1月20日由
   
    reprex
package
   
   (v0.1.1.9000)。