\begin{Schunk}
\begin{Sinput}
 nes2002$democ <- 0
 nes2002$democ[!is.na(nes2002$partyid) & nes2002$partyid == "D"] <- 1
 table(nes2002$democ, nes2002$partyid, exclude = NULL)
\end{Sinput}
\begin{Soutput}
      I   D   R <NA>
  0 429   0 474  106
  1   0 502   0    0
\end{Soutput}
\begin{Sinput}
 nes2002$repub <- 0
 nes2002$repub[!is.na(nes2002$partyid) & nes2002$partyid == "R"] <- 1
 table(nes2002$repub, nes2002$partyid, exclude = NULL, dnn = list("repub", "partyid"))
\end{Sinput}
\begin{Soutput}
     partyid
repub   I   D   R <NA>
    0 429 502   0  106
    1   0   0 474    0
\end{Soutput}
\end{Schunk}
