Friday, March 6, 2015

Code to look at ERIC updates

Under Construction
To make this code work you need to ask your election official to include the fields:

StatusReason
LastUpdateDate
UserCode1 # ERIC update flag

in your voter history.
Code without output can be found far below.

This code is intended to illustrate grouping, aggregation, tabling, cross tabulation of the 'Status Reason' field available in most WA county databases. When accompanied by the 'LastUpdateDate', 'StatusReason' gives an explanation of voter registration history. I also focus here on an "UserCode1" update flag that delineates voter status changed by "ERIC"  WA. (Electronic Registration Information Center).  Functions like aggregate, xtabs, table, subset are R stats or  base packages.  Dplyr, sqldf, and lubridate are CRAN packages.


> # detach(package:dplyr)
> # detach(package:plyr)
> library(dplyr)
> library(sqldf)
> library(lubridate)

> voterdb1 <- read.delim("C:/Politics/voterdb.02.27.2015.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)

> sqldf("Select Count(StatusReason)as count,StatusReason from voterdb1 Group By StatusReason Order By count DESC")
   count                                    StatusReason
1  17014       Third Party Change of Address (In-County)
2  16020                                                
3  15330                     Re-registration; no changes
4  14613               Added by WEI Statewide Online Reg
5  12794                           Original Registration
6  11078     A - Phone/Email update from ballot envelope
7   9857           Re-registration due to Address change
8   8162                    Updated by WEI Statewide ACS
9   7560                 Address Updated by WEI Addr Chg
10  6978                               Office Correction
11  6807                 Confirmed 3PCOA (Undeliverable)
12  5217   Third Party Change of Address (Out-of-County)
13  3313   Third Party Change of Address (Undeliverable)
14  2634                        Precinct Line Adjustment
15  1148              Re-registration due to Name change
16   882          Name Updated by WEI Statewide Addr Chg
17   749  Name/Address Updated by WEI Statewide Addr Chg
18   333                     Confirmed 3PCOA (In-County)
19   323                                Confirm by voter
20   272                    Verification Notice Returned
21   193 Z - Re-registration due to Seasonal Update Card
22   181    Re-Registration due to Name & Address Change
23   150                 Confirmed 3PCOA (Out-of-County)
24    53                  Confirmed 3PCOA (Out-of-State)
25    33                Re-registration; Signed Petition
26    26                      Z - Cancel (Inactive List)
27     5                        Cancel 45 Day No Contact
28     1                      Pending due to Citizenship
29     1         Pending due to missing Physical Address
30     1                     Registered in another State
31     1                              Requested by Voter
32     1             Z - Original Registration - No I.D.
> sqldf("Select Count(StatusReason)as count,StatusReason from voterdb1 where UserCode1 = 'ERIC' Group By StatusReason Order By count DESC")
   count                                  StatusReason
1    659     Third Party Change of Address (In-County)
2    291 Third Party Change of Address (Out-of-County)
3     19               Confirmed 3PCOA (Undeliverable)
4     16 Third Party Change of Address (Undeliverable)
5      5                              Confirm by voter
6      4                             Office Correction
7      2        Name Updated by WEI Statewide Addr Chg
8      2                   Re-registration; no changes
9      2                  Updated by WEI Statewide ACS
10     1               Address Updated by WEI Addr Chg
11     1         Re-registration due to Address change
12     1            Re-registration due to Name change
> sqldf("Select Count(StatusReason)as count,StatusReason,LastUpdateDate from voterdb1 where UserCode1 = 'ERIC' Group By StatusReason,LastUpdateDate Order By count DESC")
   count                                  StatusReason LastUpdateDate
1    389     Third Party Change of Address (In-County)       01/12/15
2    191 Third Party Change of Address (Out-of-County)       01/12/15
3    117     Third Party Change of Address (In-County)       01/14/15
4     81 Third Party Change of Address (Out-of-County)       01/14/15
5     77     Third Party Change of Address (In-County)       02/25/15
6     68     Third Party Change of Address (In-County)       01/15/15
7     11               Confirmed 3PCOA (Undeliverable)       01/27/15
8     11 Third Party Change of Address (Out-of-County)       02/25/15
9      6     Third Party Change of Address (In-County)       01/26/15
10     5 Third Party Change of Address (Undeliverable)       02/03/15
11     4                             Office Correction       01/12/15
12     3 Third Party Change of Address (Out-of-County)       01/26/15
13     3 Third Party Change of Address (Undeliverable)       02/26/15
14     2                              Confirm by voter       02/06/15
15     2               Confirmed 3PCOA (Undeliverable)       01/21/15
16     2               Confirmed 3PCOA (Undeliverable)       02/19/15
17     2     Third Party Change of Address (In-County)       02/26/15
18     2 Third Party Change of Address (Out-of-County)       01/15/15
19     2 Third Party Change of Address (Undeliverable)       02/09/15
20     2 Third Party Change of Address (Undeliverable)       02/17/15
21     1               Address Updated by WEI Addr Chg       02/17/15
22     1                              Confirm by voter       02/09/15
23     1                              Confirm by voter       02/19/15
24     1                              Confirm by voter       02/27/15
25     1               Confirmed 3PCOA (Undeliverable)       01/28/15
26     1               Confirmed 3PCOA (Undeliverable)       01/29/15
27     1               Confirmed 3PCOA (Undeliverable)       01/30/15
28     1               Confirmed 3PCOA (Undeliverable)       02/17/15
29     1        Name Updated by WEI Statewide Addr Chg       01/22/15
30     1        Name Updated by WEI Statewide Addr Chg       01/23/15
31     1         Re-registration due to Address change       02/25/15
32     1            Re-registration due to Name change       02/26/15
33     1                   Re-registration; no changes       02/03/15
34     1                   Re-registration; no changes       02/23/15
35     1 Third Party Change of Address (Out-of-County)       01/22/15
36     1 Third Party Change of Address (Out-of-County)       02/03/15
37     1 Third Party Change of Address (Out-of-County)       02/26/15
38     1 Third Party Change of Address (Undeliverable)       01/14/15
39     1 Third Party Change of Address (Undeliverable)       02/10/15
40     1 Third Party Change of Address (Undeliverable)       02/11/15
41     1 Third Party Change of Address (Undeliverable)       02/12/15
42     1                  Updated by WEI Statewide ACS       02/06/15
43     1                  Updated by WEI Statewide ACS       02/13/15

> # with aggregate
> freq <- row.names(voterdb1)
> ag <- aggregate(freq ~ StatusReason,data = voterdb1,length)
> tbl_df(arrange(ag,desc(freq)))
Source: local data frame [32 x 2]

                                  StatusReason  freq
1    Third Party Change of Address (In-County) 17014
2                                              16020
3                  Re-registration; no changes 15330
4            Added by WEI Statewide Online Reg 14613
5                        Original Registration 12794
6  A - Phone/Email update from ballot envelope 11078
7        Re-registration due to Address change  9857
8                 Updated by WEI Statewide ACS  8162
9              Address Updated by WEI Addr Chg  7560
10                           Office Correction  6978
..                                         ...   ...
> ag <- aggregate(freq ~ StatusReason + LastUpdateDate,data = voterdb1,length)
> tbl_df(arrange(ag,desc(freq)))
Source: local data frame [9,918 x 3]

                                    StatusReason LastUpdateDate  freq
1                                                      12/10/05 10949
2                                                      12/11/05  3304
3                                                      11/14/08  1522
4          Re-registration due to Address change       12/10/05  1384
5                       Precinct Line Adjustment       04/10/14  1371
6                              Office Correction       12/10/05  1304
7                          Original Registration       12/10/05  1149
8  Third Party Change of Address (Undeliverable)       12/30/14   993
9      Third Party Change of Address (In-County)       09/04/14   907
10                      Precinct Line Adjustment       04/09/14   894
..                                           ...            ...   ...

> # with xtabs
> xt <- data.frame(xtabs(~StatusReason,data = voterdb1))
> tbl_df(arrange(xt,desc(Freq)))
Source: local data frame [32 x 2]

                                  StatusReason  Freq
1    Third Party Change of Address (In-County) 17014
2                                              16020
3                  Re-registration; no changes 15330
4            Added by WEI Statewide Online Reg 14613
5                        Original Registration 12794
6  A - Phone/Email update from ballot envelope 11078
7        Re-registration due to Address change  9857
8                 Updated by WEI Statewide ACS  8162
9              Address Updated by WEI Addr Chg  7560
10                           Office Correction  6978
..                                         ...   ...
> xt <- data.frame(xtabs(~StatusReason + LastUpdateDate,data = voterdb1))
> tbl_df(arrange(xt,desc(Freq)))
Source: local data frame [64,096 x 3]

                                    StatusReason LastUpdateDate  Freq
1                                                      12/10/05 10949
2                                                      12/11/05  3304
3                                                      11/14/08  1522
4          Re-registration due to Address change       12/10/05  1384
5                       Precinct Line Adjustment       04/10/14  1371
6                              Office Correction       12/10/05  1304
7                          Original Registration       12/10/05  1149
8  Third Party Change of Address (Undeliverable)       12/30/14   993
9      Third Party Change of Address (In-County)       09/04/14   907
10                      Precinct Line Adjustment       04/09/14   894
..                                           ...            ...   ...

> # with aggregate
> t0.1 <- subset(voterdb1, StatusCode == 'A', select=StatusReason)
> t0.1df <- aggregate(t0.1,list(t0.1$StatusReason),FUN=length)
> arrange(t0.1df,desc(StatusReason))
                                           Group.1 StatusReason
1        Third Party Change of Address (In-County)        16780
2                                                         16020
3                      Re-registration; no changes        15330
4                Added by WEI Statewide Online Reg        14613
5                            Original Registration        12794
6      A - Phone/Email update from ballot envelope        11078
7            Re-registration due to Address change         9857
8                     Updated by WEI Statewide ACS         8162
9                  Address Updated by WEI Addr Chg         7560
10                               Office Correction         6907
11                        Precinct Line Adjustment         2634
12              Re-registration due to Name change         1148
13          Name Updated by WEI Statewide Addr Chg          882
14  Name/Address Updated by WEI Statewide Addr Chg          749
15                                Confirm by voter          323
16                     Confirmed 3PCOA (In-County)          318
17                    Verification Notice Returned          272
18 Z - Re-registration due to Seasonal Update Card          193
19    Re-Registration due to Name & Address Change          181
20                Re-registration; Signed Petition           33
21                      Z - Cancel (Inactive List)           26
22                        Cancel 45 Day No Contact            5
23   Third Party Change of Address (Undeliverable)            4
24                 Confirmed 3PCOA (Undeliverable)            2
25   Third Party Change of Address (Out-of-County)            2
26                 Confirmed 3PCOA (Out-of-County)            1
27                      Pending due to Citizenship            1
28         Pending due to missing Physical Address            1
29                     Registered in another State            1
30                              Requested by Voter            1
31             Z - Original Registration - No I.D.            1

> t0.2 <- subset(voterdb1, StatusCode == 'I', select=StatusReason)
> t0.2df <- aggregate(t0.2,list(t0.2$StatusReason),FUN=length)
> arrange(t0.2df,desc(StatusReason))
                                        Group.1 StatusReason
1               Confirmed 3PCOA (Undeliverable)         6805
2 Third Party Change of Address (Out-of-County)         5215
3 Third Party Change of Address (Undeliverable)         3309
4     Third Party Change of Address (In-County)          234
5               Confirmed 3PCOA (Out-of-County)          149
6                             Office Correction           71
7                Confirmed 3PCOA (Out-of-State)           53
8                   Confirmed 3PCOA (In-County)           15

> t0.3 <- subset(voterdb1, UserCode1 == 'ERIC' & StatusCode == 'A', select=StatusReason)
> t0.3df <- aggregate(t0.3,list(t0.3$StatusReason),FUN=length)
> arrange(t0.3df,desc(StatusReason))
                                         Group.1 StatusReason
1      Third Party Change of Address (In-County)          426
2                               Confirm by voter            5
3                              Office Correction            4
4         Name Updated by WEI Statewide Addr Chg            2
5                    Re-registration; no changes            2
6                   Updated by WEI Statewide ACS            2
7                Address Updated by WEI Addr Chg            1
8          Re-registration due to Address change            1
9             Re-registration due to Name change            1
10 Third Party Change of Address (Out-of-County)            1

> t0.4 <- subset(voterdb1, UserCode1 == 'ERIC' & StatusCode == 'I', select=StatusReason)
> t0.4df <- aggregate(t0.4,list(t0.4$StatusReason),FUN=length)
> arrange(t0.4df,desc(StatusReason))
                                        Group.1 StatusReason
1 Third Party Change of Address (Out-of-County)          290
2     Third Party Change of Address (In-County)          233
3               Confirmed 3PCOA (Undeliverable)           19
4 Third Party Change of Address (Undeliverable)           16

> t0.5 <- subset(voterdb1, UserCode1 == 'ERIC', select=StatusReason)
> t0.5df <- aggregate(t0.5,list(t0.5$StatusReason),FUN=length)
> arrange(t0.5df,desc(StatusReason))
                                         Group.1 StatusReason
1      Third Party Change of Address (In-County)          659
2  Third Party Change of Address (Out-of-County)          291
3                Confirmed 3PCOA (Undeliverable)           19
4  Third Party Change of Address (Undeliverable)           16
5                               Confirm by voter            5
6                              Office Correction            4
7         Name Updated by WEI Statewide Addr Chg            2
8                    Re-registration; no changes            2
9                   Updated by WEI Statewide ACS            2
10               Address Updated by WEI Addr Chg            1
11         Re-registration due to Address change            1
12            Re-registration due to Name change            1

> # with xtabs
> t0.1x <- subset(voterdb1, StatusCode == 'A', select=StatusReason)
> t0.1xdf <- data.frame(xtabs(~StatusReason,data=t0.1x))
> arrange(t0.1xdf,desc(Freq))
                                      StatusReason  Freq
1        Third Party Change of Address (In-County) 16780
2                                                  16020
3                      Re-registration; no changes 15330
4                Added by WEI Statewide Online Reg 14613
5                            Original Registration 12794
6      A - Phone/Email update from ballot envelope 11078
7            Re-registration due to Address change  9857
8                     Updated by WEI Statewide ACS  8162
9                  Address Updated by WEI Addr Chg  7560
10                               Office Correction  6907
11                        Precinct Line Adjustment  2634
12              Re-registration due to Name change  1148
13          Name Updated by WEI Statewide Addr Chg   882
14  Name/Address Updated by WEI Statewide Addr Chg   749
15                                Confirm by voter   323
16                     Confirmed 3PCOA (In-County)   318
17                    Verification Notice Returned   272
18 Z - Re-registration due to Seasonal Update Card   193
19    Re-Registration due to Name & Address Change   181
20                Re-registration; Signed Petition    33
21                      Z - Cancel (Inactive List)    26
22                        Cancel 45 Day No Contact     5
23   Third Party Change of Address (Undeliverable)     4
24                 Confirmed 3PCOA (Undeliverable)     2
25   Third Party Change of Address (Out-of-County)     2
26                 Confirmed 3PCOA (Out-of-County)     1
27                      Pending due to Citizenship     1
28         Pending due to missing Physical Address     1
29                     Registered in another State     1
30                              Requested by Voter     1
31             Z - Original Registration - No I.D.     1

> t0.2x <- subset(voterdb1, StatusCode == 'I', select=StatusReason)
> t0.2xdf <- data.frame(xtabs(~StatusReason,data=t0.2x))
> arrange(t0.2xdf,desc(Freq))
                                   StatusReason Freq
1               Confirmed 3PCOA (Undeliverable) 6805
2 Third Party Change of Address (Out-of-County) 5215
3 Third Party Change of Address (Undeliverable) 3309
4     Third Party Change of Address (In-County)  234
5               Confirmed 3PCOA (Out-of-County)  149
6                             Office Correction   71
7                Confirmed 3PCOA (Out-of-State)   53
8                   Confirmed 3PCOA (In-County)   15

> t0.3x <- subset(voterdb1, UserCode1 == 'ERIC' & StatusCode == 'A', select=StatusReason)
> t0.3xdf <- data.frame(xtabs(~StatusReason,data=t0.3x))
> arrange(t0.3xdf,desc(Freq))
                                    StatusReason Freq
1      Third Party Change of Address (In-County)  426
2                               Confirm by voter    5
3                              Office Correction    4
4         Name Updated by WEI Statewide Addr Chg    2
5                    Re-registration; no changes    2
6                   Updated by WEI Statewide ACS    2
7                Address Updated by WEI Addr Chg    1
8          Re-registration due to Address change    1
9             Re-registration due to Name change    1
10 Third Party Change of Address (Out-of-County)    1

> t0.4x <- subset(voterdb1, UserCode1 == 'ERIC' & StatusCode == 'I', select=StatusReason)
> t0.4xdf <- data.frame(xtabs(~StatusReason,data=t0.4x))
> arrange(t0.4xdf,desc(Freq))
                                   StatusReason Freq
1 Third Party Change of Address (Out-of-County)  290
2     Third Party Change of Address (In-County)  233
3               Confirmed 3PCOA (Undeliverable)   19
4 Third Party Change of Address (Undeliverable)   16

> t0.5x <- subset(voterdb1, UserCode1 == 'ERIC', select=StatusReason)
> t0.5xdf <- data.frame(xtabs(~StatusReason,data=t0.5x))
> arrange(t0.5xdf,desc(Freq))
                                    StatusReason Freq
1      Third Party Change of Address (In-County)  659
2  Third Party Change of Address (Out-of-County)  291
3                Confirmed 3PCOA (Undeliverable)   19
4  Third Party Change of Address (Undeliverable)   16
5                               Confirm by voter    5
6                              Office Correction    4
7         Name Updated by WEI Statewide Addr Chg    2
8                    Re-registration; no changes    2
9                   Updated by WEI Statewide ACS    2
10               Address Updated by WEI Addr Chg    1
11         Re-registration due to Address change    1
12            Re-registration due to Name change    1

> # library(plyr)
> # plyr::arrange(plyr::count(voterdb1$StatusReason),desc(freq))
> # plyr::arrange(plyr::count(select(filter(voterdb1,UserCode1 == 'ERIC'),StatusReason)),desc(freq))

> data.frame(count(voterdb1,StatusReason,sort=TRUE))
                                      StatusReason     n
1        Third Party Change of Address (In-County) 17014
2                                                  16020
3                      Re-registration; no changes 15330
4                Added by WEI Statewide Online Reg 14613
5                            Original Registration 12794
6      A - Phone/Email update from ballot envelope 11078
7            Re-registration due to Address change  9857
8                     Updated by WEI Statewide ACS  8162
9                  Address Updated by WEI Addr Chg  7560
10                               Office Correction  6978
11                 Confirmed 3PCOA (Undeliverable)  6807
12   Third Party Change of Address (Out-of-County)  5217
13   Third Party Change of Address (Undeliverable)  3313
14                        Precinct Line Adjustment  2634
15              Re-registration due to Name change  1148
16          Name Updated by WEI Statewide Addr Chg   882
17  Name/Address Updated by WEI Statewide Addr Chg   749
18                     Confirmed 3PCOA (In-County)   333
19                                Confirm by voter   323
20                    Verification Notice Returned   272
21 Z - Re-registration due to Seasonal Update Card   193
22    Re-Registration due to Name & Address Change   181
23                 Confirmed 3PCOA (Out-of-County)   150
24                  Confirmed 3PCOA (Out-of-State)    53
25                Re-registration; Signed Petition    33
26                      Z - Cancel (Inactive List)    26
27                        Cancel 45 Day No Contact     5
28                      Pending due to Citizenship     1
29         Pending due to missing Physical Address     1
30                     Registered in another State     1
31                              Requested by Voter     1
32             Z - Original Registration - No I.D.     1
> data.frame(count(filter(voterdb1,UserCode1 == 'ERIC'),StatusReason,sort=TRUE))
                                    StatusReason   n
1      Third Party Change of Address (In-County) 659
2  Third Party Change of Address (Out-of-County) 291
3                Confirmed 3PCOA (Undeliverable)  19
4  Third Party Change of Address (Undeliverable)  16
5                               Confirm by voter   5
6                              Office Correction   4
7         Name Updated by WEI Statewide Addr Chg   2
8                    Re-registration; no changes   2
9                   Updated by WEI Statewide ACS   2
10               Address Updated by WEI Addr Chg   1
11         Re-registration due to Address change   1
12            Re-registration due to Name change   1

> write.csv(with(voterdb1,table(StatusReason,LastUpdateDate,UserCode1)),"table.csv",row.names=FALSE)
> t1 <- read.csv("table.csv")
> t1$LastUpdateDate <- mdy(t1$LastUpdateDate)
> arrange(filter(t1,UserCode1 == 'ERIC' & Freq != 0), desc(Freq))
                                    StatusReason LastUpdateDate UserCode1 Freq
1      Third Party Change of Address (In-County)     2015-01-12      ERIC  389
2  Third Party Change of Address (Out-of-County)     2015-01-12      ERIC  191
3      Third Party Change of Address (In-County)     2015-01-14      ERIC  117
4  Third Party Change of Address (Out-of-County)     2015-01-14      ERIC   81
5      Third Party Change of Address (In-County)     2015-02-25      ERIC   77
6      Third Party Change of Address (In-County)     2015-01-15      ERIC   68
7                Confirmed 3PCOA (Undeliverable)     2015-01-27      ERIC   11
8  Third Party Change of Address (Out-of-County)     2015-02-25      ERIC   11
9      Third Party Change of Address (In-County)     2015-01-26      ERIC    6
10 Third Party Change of Address (Undeliverable)     2015-02-03      ERIC    5
11                             Office Correction     2015-01-12      ERIC    4
12 Third Party Change of Address (Out-of-County)     2015-01-26      ERIC    3
13 Third Party Change of Address (Undeliverable)     2015-02-26      ERIC    3
14 Third Party Change of Address (Out-of-County)     2015-01-15      ERIC    2
15               Confirmed 3PCOA (Undeliverable)     2015-01-21      ERIC    2
16                              Confirm by voter     2015-02-06      ERIC    2
17 Third Party Change of Address (Undeliverable)     2015-02-09      ERIC    2
18 Third Party Change of Address (Undeliverable)     2015-02-17      ERIC    2
19               Confirmed 3PCOA (Undeliverable)     2015-02-19      ERIC    2
20     Third Party Change of Address (In-County)     2015-02-26      ERIC    2
21 Third Party Change of Address (Undeliverable)     2015-01-14      ERIC    1
22        Name Updated by WEI Statewide Addr Chg     2015-01-22      ERIC    1
23 Third Party Change of Address (Out-of-County)     2015-01-22      ERIC    1
24        Name Updated by WEI Statewide Addr Chg     2015-01-23      ERIC    1
25               Confirmed 3PCOA (Undeliverable)     2015-01-28      ERIC    1
26               Confirmed 3PCOA (Undeliverable)     2015-01-29      ERIC    1
27               Confirmed 3PCOA (Undeliverable)     2015-01-30      ERIC    1
28                   Re-registration; no changes     2015-02-03      ERIC    1
29 Third Party Change of Address (Out-of-County)     2015-02-03      ERIC    1
30                  Updated by WEI Statewide ACS     2015-02-06      ERIC    1
31                              Confirm by voter     2015-02-09      ERIC    1
32 Third Party Change of Address (Undeliverable)     2015-02-10      ERIC    1
33 Third Party Change of Address (Undeliverable)     2015-02-11      ERIC    1
34 Third Party Change of Address (Undeliverable)     2015-02-12      ERIC    1
35                  Updated by WEI Statewide ACS     2015-02-13      ERIC    1
36               Address Updated by WEI Addr Chg     2015-02-17      ERIC    1
37               Confirmed 3PCOA (Undeliverable)     2015-02-17      ERIC    1
38                              Confirm by voter     2015-02-19      ERIC    1
39                   Re-registration; no changes     2015-02-23      ERIC    1
40         Re-registration due to Address change     2015-02-25      ERIC    1
41            Re-registration due to Name change     2015-02-26      ERIC    1
42 Third Party Change of Address (Out-of-County)     2015-02-26      ERIC    1
43                              Confirm by voter     2015-02-27      ERIC    1
> arrange(filter(t1,UserCode1 == 'ERIC' & Freq != 0), desc(LastUpdateDate))
                                    StatusReason LastUpdateDate UserCode1 Freq
1                               Confirm by voter     2015-02-27      ERIC    1
2             Re-registration due to Name change     2015-02-26      ERIC    1
3      Third Party Change of Address (In-County)     2015-02-26      ERIC    2
4  Third Party Change of Address (Out-of-County)     2015-02-26      ERIC    1
5  Third Party Change of Address (Undeliverable)     2015-02-26      ERIC    3
6          Re-registration due to Address change     2015-02-25      ERIC    1
7      Third Party Change of Address (In-County)     2015-02-25      ERIC   77
8  Third Party Change of Address (Out-of-County)     2015-02-25      ERIC   11
9                    Re-registration; no changes     2015-02-23      ERIC    1
10                              Confirm by voter     2015-02-19      ERIC    1
11               Confirmed 3PCOA (Undeliverable)     2015-02-19      ERIC    2
12               Address Updated by WEI Addr Chg     2015-02-17      ERIC    1
13               Confirmed 3PCOA (Undeliverable)     2015-02-17      ERIC    1
14 Third Party Change of Address (Undeliverable)     2015-02-17      ERIC    2
15                  Updated by WEI Statewide ACS     2015-02-13      ERIC    1
16 Third Party Change of Address (Undeliverable)     2015-02-12      ERIC    1
17 Third Party Change of Address (Undeliverable)     2015-02-11      ERIC    1
18 Third Party Change of Address (Undeliverable)     2015-02-10      ERIC    1
19                              Confirm by voter     2015-02-09      ERIC    1
20 Third Party Change of Address (Undeliverable)     2015-02-09      ERIC    2
21                              Confirm by voter     2015-02-06      ERIC    2
22                  Updated by WEI Statewide ACS     2015-02-06      ERIC    1
23                   Re-registration; no changes     2015-02-03      ERIC    1
24 Third Party Change of Address (Out-of-County)     2015-02-03      ERIC    1
25 Third Party Change of Address (Undeliverable)     2015-02-03      ERIC    5
26               Confirmed 3PCOA (Undeliverable)     2015-01-30      ERIC    1
27               Confirmed 3PCOA (Undeliverable)     2015-01-29      ERIC    1
28               Confirmed 3PCOA (Undeliverable)     2015-01-28      ERIC    1
29               Confirmed 3PCOA (Undeliverable)     2015-01-27      ERIC   11
30     Third Party Change of Address (In-County)     2015-01-26      ERIC    6
31 Third Party Change of Address (Out-of-County)     2015-01-26      ERIC    3
32        Name Updated by WEI Statewide Addr Chg     2015-01-23      ERIC    1
33        Name Updated by WEI Statewide Addr Chg     2015-01-22      ERIC    1
34 Third Party Change of Address (Out-of-County)     2015-01-22      ERIC    1
35               Confirmed 3PCOA (Undeliverable)     2015-01-21      ERIC    2
36     Third Party Change of Address (In-County)     2015-01-15      ERIC   68
37 Third Party Change of Address (Out-of-County)     2015-01-15      ERIC    2
38     Third Party Change of Address (In-County)     2015-01-14      ERIC  117
39 Third Party Change of Address (Out-of-County)     2015-01-14      ERIC   81
40 Third Party Change of Address (Undeliverable)     2015-01-14      ERIC    1
41                             Office Correction     2015-01-12      ERIC    4
42     Third Party Change of Address (In-County)     2015-01-12      ERIC  389
43 Third Party Change of Address (Out-of-County)     2015-01-12      ERIC  191

> t2 <- 
+  voterdb1 %>% 
+  filter(UserCode1 == 'ERIC') %>%
+  select(StatusReason,LastUpdateDate,UserCode1)
> count(t2,StatusReason,sort = TRUE)
Source: local data frame [12 x 2]

                                    StatusReason   n
1      Third Party Change of Address (In-County) 659
2  Third Party Change of Address (Out-of-County) 291
3                Confirmed 3PCOA (Undeliverable)  19
4  Third Party Change of Address (Undeliverable)  16
5                               Confirm by voter   5
6                              Office Correction   4
7         Name Updated by WEI Statewide Addr Chg   2
8                    Re-registration; no changes   2
9                   Updated by WEI Statewide ACS   2
10               Address Updated by WEI Addr Chg   1
11         Re-registration due to Address change   1
12            Re-registration due to Name change   1

> t3 <- filter(t1,UserCode1 == 'ERIC')
> n <- count(t3,Freq != 0)[2,2]
> arrange(head(arrange(t3,desc(Freq)),n),desc(Freq))
                                    StatusReason LastUpdateDate UserCode1 Freq
1      Third Party Change of Address (In-County)     2015-01-12      ERIC  389
2  Third Party Change of Address (Out-of-County)     2015-01-12      ERIC  191
3      Third Party Change of Address (In-County)     2015-01-14      ERIC  117
4  Third Party Change of Address (Out-of-County)     2015-01-14      ERIC   81
5      Third Party Change of Address (In-County)     2015-02-25      ERIC   77
6      Third Party Change of Address (In-County)     2015-01-15      ERIC   68
7                Confirmed 3PCOA (Undeliverable)     2015-01-27      ERIC   11
8  Third Party Change of Address (Out-of-County)     2015-02-25      ERIC   11
9      Third Party Change of Address (In-County)     2015-01-26      ERIC    6
10 Third Party Change of Address (Undeliverable)     2015-02-03      ERIC    5
11                             Office Correction     2015-01-12      ERIC    4
12 Third Party Change of Address (Out-of-County)     2015-01-26      ERIC    3
13 Third Party Change of Address (Undeliverable)     2015-02-26      ERIC    3
14 Third Party Change of Address (Out-of-County)     2015-01-15      ERIC    2
15               Confirmed 3PCOA (Undeliverable)     2015-01-21      ERIC    2
16                              Confirm by voter     2015-02-06      ERIC    2
17 Third Party Change of Address (Undeliverable)     2015-02-09      ERIC    2
18 Third Party Change of Address (Undeliverable)     2015-02-17      ERIC    2
19               Confirmed 3PCOA (Undeliverable)     2015-02-19      ERIC    2
20     Third Party Change of Address (In-County)     2015-02-26      ERIC    2
21 Third Party Change of Address (Undeliverable)     2015-01-14      ERIC    1
22        Name Updated by WEI Statewide Addr Chg     2015-01-22      ERIC    1
23 Third Party Change of Address (Out-of-County)     2015-01-22      ERIC    1
24        Name Updated by WEI Statewide Addr Chg     2015-01-23      ERIC    1
25               Confirmed 3PCOA (Undeliverable)     2015-01-28      ERIC    1
26               Confirmed 3PCOA (Undeliverable)     2015-01-29      ERIC    1
27               Confirmed 3PCOA (Undeliverable)     2015-01-30      ERIC    1
28                   Re-registration; no changes     2015-02-03      ERIC    1
29 Third Party Change of Address (Out-of-County)     2015-02-03      ERIC    1
30                  Updated by WEI Statewide ACS     2015-02-06      ERIC    1
31                              Confirm by voter     2015-02-09      ERIC    1
32 Third Party Change of Address (Undeliverable)     2015-02-10      ERIC    1
33 Third Party Change of Address (Undeliverable)     2015-02-11      ERIC    1
34 Third Party Change of Address (Undeliverable)     2015-02-12      ERIC    1
35                  Updated by WEI Statewide ACS     2015-02-13      ERIC    1
36               Address Updated by WEI Addr Chg     2015-02-17      ERIC    1
37               Confirmed 3PCOA (Undeliverable)     2015-02-17      ERIC    1
38                              Confirm by voter     2015-02-19      ERIC    1
39                   Re-registration; no changes     2015-02-23      ERIC    1
40         Re-registration due to Address change     2015-02-25      ERIC    1
41            Re-registration due to Name change     2015-02-26      ERIC    1
42 Third Party Change of Address (Out-of-County)     2015-02-26      ERIC    1
43                              Confirm by voter     2015-02-27      ERIC    1

> freq <- as.numeric(row.names(voterdb1))
> tbl_df(aggregate(freq ~ StatusReason,data = voterdb1,length))
Source: local data frame [32 x 2]

                                  StatusReason  freq
1                                              16020
2  A - Phone/Email update from ballot envelope 11078
3            Added by WEI Statewide Online Reg 14613
4              Address Updated by WEI Addr Chg  7560
5                     Cancel 45 Day No Contact     5
6                             Confirm by voter   323
7                  Confirmed 3PCOA (In-County)   333
8              Confirmed 3PCOA (Out-of-County)   150
9               Confirmed 3PCOA (Out-of-State)    53
10             Confirmed 3PCOA (Undeliverable)  6807
..                                         ...   ...
> tbl_df(data.frame(xtabs(~StatusReason,data = voterdb1)))
Source: local data frame [32 x 2]

                                  StatusReason  Freq
1                                              16020
2  A - Phone/Email update from ballot envelope 11078
3            Added by WEI Statewide Online Reg 14613
4              Address Updated by WEI Addr Chg  7560
5                     Cancel 45 Day No Contact     5
6                             Confirm by voter   323
7                  Confirmed 3PCOA (In-County)   333
8              Confirmed 3PCOA (Out-of-County)   150
9               Confirmed 3PCOA (Out-of-State)    53
10             Confirmed 3PCOA (Undeliverable)  6807
..                                         ...   ...
>                                    
> freq <- as.numeric(row.names(voterdb1))
> arrange(data.frame(with(voterdb1,table(BallotCounted_5,StatusReason))),desc(BallotCounted_5),desc(Freq))
   BallotCounted_5                                    StatusReason  Freq
1                1                                                 13742
2                1                     Re-registration; no changes 12590
3                1       Third Party Change of Address (In-County)  8180
4                1     A - Phone/Email update from ballot envelope  8064
5                1           Re-registration due to Address change  6868
6                1                    Updated by WEI Statewide ACS  4942
7                1                               Office Correction  4679
8                1                           Original Registration  4662
9                1                 Address Updated by WEI Addr Chg  3637
10               1               Added by WEI Statewide Online Reg  1793
11               1   Third Party Change of Address (Out-of-County)  1698
12               1                        Precinct Line Adjustment  1623
13               1                 Confirmed 3PCOA (Undeliverable)  1264
14               1              Re-registration due to Name change   904
15               1   Third Party Change of Address (Undeliverable)   458
16               1          Name Updated by WEI Statewide Addr Chg   456
17               1  Name/Address Updated by WEI Statewide Addr Chg   343
18               1                     Confirmed 3PCOA (In-County)   209
19               1                    Verification Notice Returned   165
20               1 Z - Re-registration due to Seasonal Update Card   145
21               1                                Confirm by voter   108
22               1    Re-Registration due to Name & Address Change    99
23               1                 Confirmed 3PCOA (Out-of-County)    47
24               1                      Z - Cancel (Inactive List)    22
25               1                  Confirmed 3PCOA (Out-of-State)    18
26               1                Re-registration; Signed Petition    12
27               1                        Cancel 45 Day No Contact     4
28               1                      Pending due to Citizenship     1
29               1                     Registered in another State     1
30               1         Pending due to missing Physical Address     0
31               1                              Requested by Voter     0
32               1             Z - Original Registration - No I.D.     0
33               0       Third Party Change of Address (In-County)  5037
34               0                 Confirmed 3PCOA (Undeliverable)  2582
35               0                                                  2278
36               0                           Original Registration  1798
37               0                    Updated by WEI Statewide ACS  1649
38               0   Third Party Change of Address (Out-of-County)  1591
39               0           Re-registration due to Address change  1562
40               0                 Address Updated by WEI Addr Chg  1518
41               0                     Re-registration; no changes  1447
42               0                               Office Correction  1415
43               0               Added by WEI Statewide Online Reg  1116
44               0   Third Party Change of Address (Undeliverable)  1008
45               0     A - Phone/Email update from ballot envelope   689
46               0                        Precinct Line Adjustment   523
47               0  Name/Address Updated by WEI Statewide Addr Chg   241
48               0          Name Updated by WEI Statewide Addr Chg   236
49               0              Re-registration due to Name change   189
50               0                     Confirmed 3PCOA (In-County)    62
51               0    Re-Registration due to Name & Address Change    46
52               0                                Confirm by voter    39
53               0                    Verification Notice Returned    20
54               0                 Confirmed 3PCOA (Out-of-County)    16
55               0 Z - Re-registration due to Seasonal Update Card    16
56               0                  Confirmed 3PCOA (Out-of-State)    14
57               0                Re-registration; Signed Petition    14
58               0                      Z - Cancel (Inactive List)     2
59               0                              Requested by Voter     1
60               0             Z - Original Registration - No I.D.     1
61               0                        Cancel 45 Day No Contact     0
62               0                      Pending due to Citizenship     0
63               0         Pending due to missing Physical Address     0
64               0                     Registered in another State     0

> voterdbA <- subset(voterdb1, StatusCode == 'A')
> # useNA="na" ; default
> #BP <- arrange(data.frame(with(voterdbA,table(BallotCounted_1,BallotCounted_2,BallotCounted_3,BallotCounted_4,BallotCounted_5))),desc(Freq))
> # useNA = "always"; 
> BP <- arrange(data.frame(with(voterdbA,table(BallotCounted_1,BallotCounted_2,BallotCounted_3,BallotCounted_4,BallotCounted_5,useNA = "always"))),desc(Freq))
> BP$BallotCounted_1 <- as.numeric(as.character(BP$BallotCounted_1))
> BP$BallotCounted_2 <- as.numeric(as.character(BP$BallotCounted_2))
> BP$BallotCounted_3 <- as.numeric(as.character(BP$BallotCounted_3))
> BP$BallotCounted_4 <- as.numeric(as.character(BP$BallotCounted_4))
> BP$BallotCounted_5 <- as.numeric(as.character(BP$BallotCounted_5))
> # for(i in 1:nrow(BP)) {print(cat(sub("","",BP[i,1:5])))}
> BP$RowSums <- as.integer(rowSums(BP[,1:5],na.rm=TRUE)) 
> colSums(BP, na.rm=TRUE)
BallotCounted_1 BallotCounted_2 BallotCounted_3 BallotCounted_4 BallotCounted_5            Freq         RowSums 
             81              81              81              81              81          125879             405 
> # For BPNA
> with(BP[1:23,],barplot(Freq))
> # For BP
> with(BP,barplot(Freq))
> BP[1:23,]
   BallotCounted_1 BallotCounted_2 BallotCounted_3 BallotCounted_4 BallotCounted_5  Freq RowSums
1                1               1               1               1               1 43938       5
2                0               0               0               0               0  7409       0
3                1               0               1               1               1  5018       4
4                1               1               1               0               1  4881       4
5                0              NA              NA              NA              NA  4702       0
6                0               0               1               0               0  4655       1
7                0               0               1               0               1  4324       2
8                1              NA              NA              NA              NA  3681       1
9                0               1               1               1               1  3494       4
10               1               0               1               0               1  3286       3
11               0               0               1              NA              NA  3154       1
12               0               0              NA              NA              NA  3079       0
13               0               0               1               1               1  3031       3
14               1               1               1              NA              NA  2729       3
15              NA              NA              NA              NA              NA  2510       0
16               0               0               0              NA              NA  1693       0
17               0               1               1               0               1  1670       3
18               1               1              NA              NA              NA  1649       2
19               0               0               0               0               1  1464       1
20               1               0               1              NA              NA  1449       2
21               1               0               1               0               0  1368       2
22               1               1               1               1              NA  1139       4
23               0               0               0               0              NA  1125       0


# Code without output
# detach(package:dplyr)
# detach(package:plyr)
library(dplyr)
library(sqldf)
library(lubridate)

voterdb1 <- read.delim("C:/Politics/voterdb.02.27.2015.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)

# with sqldf
sqldf("Select Count(StatusReason)as count,StatusReason from voterdb1 Group By StatusReason Order By count DESC")
sqldf("Select Count(StatusReason)as count,StatusReason from voterdb1 where UserCode1 = 'ERIC' Group By StatusReason Order By count DESC")
sqldf("Select Count(StatusReason)as count,StatusReason,LastUpdateDate from voterdb1 where UserCode1 = 'ERIC' Group By StatusReason,LastUpdateDate Order By count DESC")

# with aggregate
freq <- row.names(voterdb1)
ag <- aggregate(freq ~ StatusReason,data = voterdb1,length)
tbl_df(arrange(ag,desc(freq)))
ag <- aggregate(freq ~ StatusReason + LastUpdateDate,data = voterdb1,length)
tbl_df(arrange(ag,desc(freq)))

# with xtabs
xt <- data.frame(xtabs(~StatusReason,data = voterdb1))
tbl_df(arrange(xt,desc(Freq)))
xt <- data.frame(xtabs(~StatusReason + LastUpdateDate,data = voterdb1))
tbl_df(arrange(xt,desc(Freq)))

# with aggregate
t0.1 <- subset(voterdb1, StatusCode == 'A', select=StatusReason)
t0.1df <- aggregate(t0.1,list(t0.1$StatusReason),FUN=length)
arrange(t0.1df,desc(StatusReason))

t0.2 <- subset(voterdb1, StatusCode == 'I', select=StatusReason)
t0.2df <- aggregate(t0.2,list(t0.2$StatusReason),FUN=length)
arrange(t0.2df,desc(StatusReason))

t0.3 <- subset(voterdb1, UserCode1 == 'ERIC' & StatusCode == 'A', select=StatusReason)
t0.3df <- aggregate(t0.3,list(t0.3$StatusReason),FUN=length)
arrange(t0.3df,desc(StatusReason))

t0.4 <- subset(voterdb1, UserCode1 == 'ERIC' & StatusCode == 'I', select=StatusReason)
t0.4df <- aggregate(t0.4,list(t0.4$StatusReason),FUN=length)
arrange(t0.4df,desc(StatusReason))

t0.5 <- subset(voterdb1, UserCode1 == 'ERIC', select=StatusReason)
t0.5df <- aggregate(t0.5,list(t0.5$StatusReason),FUN=length)
arrange(t0.5df,desc(StatusReason))

# with xtabs
t0.1x <- subset(voterdb1, StatusCode == 'A', select=StatusReason)
t0.1xdf <- data.frame(xtabs(~StatusReason,data=t0.1x))
arrange(t0.1xdf,desc(Freq))

t0.2x <- subset(voterdb1, StatusCode == 'I', select=StatusReason)
t0.2xdf <- data.frame(xtabs(~StatusReason,data=t0.2x))
arrange(t0.2xdf,desc(Freq))

t0.3x <- subset(voterdb1, UserCode1 == 'ERIC' & StatusCode == 'A', select=StatusReason)
t0.3xdf <- data.frame(xtabs(~StatusReason,data=t0.3x))
arrange(t0.3xdf,desc(Freq))

t0.4x <- subset(voterdb1, UserCode1 == 'ERIC' & StatusCode == 'I', select=StatusReason)
t0.4xdf <- data.frame(xtabs(~StatusReason,data=t0.4x))
arrange(t0.4xdf,desc(Freq))

t0.5x <- subset(voterdb1, UserCode1 == 'ERIC', select=StatusReason)
t0.5xdf <- data.frame(xtabs(~StatusReason,data=t0.5x))
arrange(t0.5xdf,desc(Freq))

# library(plyr)
# plyr::arrange(plyr::count(voterdb1$StatusReason),desc(freq))
# plyr::arrange(plyr::count(select(filter(voterdb1,UserCode1 == 'ERIC'),StatusReason)),desc(freq))

data.frame(count(voterdb1,StatusReason,sort=TRUE))
data.frame(count(filter(voterdb1,UserCode1 == 'ERIC'),StatusReason,sort=TRUE))

# with table
write.csv(with(voterdb1,table(StatusReason,LastUpdateDate,UserCode1)),"table.csv",row.names=FALSE)
t1 <- read.csv("table.csv")
t1$LastUpdateDate <- mdy(t1$LastUpdateDate)
arrange(filter(t1,UserCode1 == 'ERIC' & Freq != 0), desc(Freq))
arrange(filter(t1,UserCode1 == 'ERIC' & Freq != 0), desc(LastUpdateDate))

t2 <- 
 voterdb1 %>% 
 filter(UserCode1 == 'ERIC') %>%
 select(StatusReason,LastUpdateDate,UserCode1)
count(t2,StatusReason,sort = TRUE)

t3 <- filter(t1,UserCode1 == 'ERIC')
n <- count(t3,Freq != 0)[2,2]
arrange(head(arrange(t3,desc(Freq)),n),desc(Freq))

freq <- as.numeric(row.names(voterdb1))
tbl_df(aggregate(freq ~ StatusReason,data = voterdb1,length))
tbl_df(data.frame(xtabs(~StatusReason,data = voterdb1)))
                                   
freq <- as.numeric(row.names(voterdb1))
arrange(data.frame(with(voterdb1,table(BallotCounted_5,StatusReason))),desc(BallotCounted_5),desc(Freq))

voterdbA <- subset(voterdb1, StatusCode == 'A')
# useNA="na" ; default
#BP <- arrange(data.frame(with(voterdbA,table(BallotCounted_1,BallotCounted_2,BallotCounted_3,BallotCounted_4,BallotCounted_5))),desc(Freq))
# useNA = "always"; 
BP <- arrange(data.frame(with(voterdbA,table(BallotCounted_1,BallotCounted_2,BallotCounted_3,BallotCounted_4,BallotCounted_5,useNA = "always"))),desc(Freq))
BP$BallotCounted_1 <- as.numeric(as.character(BP$BallotCounted_1))
BP$BallotCounted_2 <- as.numeric(as.character(BP$BallotCounted_2))
BP$BallotCounted_3 <- as.numeric(as.character(BP$BallotCounted_3))
BP$BallotCounted_4 <- as.numeric(as.character(BP$BallotCounted_4))
BP$BallotCounted_5 <- as.numeric(as.character(BP$BallotCounted_5))
# for(i in 1:nrow(BP)) {print(cat(sub("","",BP[i,1:5])))}
BP$RowSums <- as.integer(rowSums(BP[,1:5],na.rm=TRUE)) 
colSums(BP, na.rm=TRUE)
# For BPNA
with(BP[1:23,],barplot(Freq))
# For BP
with(BP,barplot(Freq))
BP[1:23,]

No comments:

Post a Comment