Good point.  Sorry about that. The data just looks like this:

> head(data)
   region        constituency                   party year                         name                vote
1 ASHANTI       ADANSI ASOKWA   CPP 2008        OWUSU-BOAMAH FRANCIS    314
2 ASHANTI       ADANSI ASOKWA   DFP 2008               AMOAKO ANAAFI   1332
3 ASHANTI       ADANSI ASOKWA   NDC 2008 ALHAJI ABDUL-LATEEF MADJOUB   9219
4 ASHANTI       ADANSI ASOKWA   NPP 2008         KOBINA TAHIR HAMMON  13659
5 ASHANTI AFIGYA-SEKYERE EAST   CPP 2008     OBENG NYANTAKYI CLEMENT    640
6 ASHANTI AFIGYA-SEKYERE EAST   DPP 2008           AMIDU ALHAJI ADAM    127
                                     source                        rejected
1 EC pdf from http://www.ec.gov.gh/node/208      452
2 EC pdf from http://www.ec.gov.gh/node/208      452
3 EC pdf from http://www.ec.gov.gh/node/208      452
4 EC pdf from http://www.ec.gov.gh/node/208      452
5 EC pdf from http://www.ec.gov.gh/node/208      334
6 EC pdf from http://www.ec.gov.gh/node/208      334

On Tue, May 31, 2011 at 2:35 PM, Brandon Stewart <brandonmstewart@gmail.com> wrote:
Julie,

Some comments on what is in the dataset or a small sample data frame would help a bunch.

Brandon

On Tue, May 31, 2011 at 10:33 AM, Julie Faller <jfaller@fas.harvard.edu> wrote:
Hello to anyone who is still reading this,

I am working with elections data in which I have the votes a candidate
received but not if they won or not. I want  to add a couple columns
to my dataset just noting if the candidate won and by how much. I feel
like this should be easy.  And yet…

Here is what I've done

length(unique(data$constituency))
mat<-as.vector(unique(data$constituency))

for(i in 1:229){
       sub<-data[data$constituency==mat[i],]
       ordered<-sub[order(sub[,"vote"], decreasing=TRUE),]
       win<-ordered[1,]
       second<-ordered[2,]
       totalvotes<-sum(sub[,"vote"])
       winvote<-win[,"vote"]
       secvote<-second[,"vote"]
       margin<-winvote-secvote
       pcntmarg<-as.numeric(margin/totalvotes)
       data[data[,"name"]==win[,"name"],]$marginwin[]<-pcntmarg
       data[data[,"name"]==win[,"name"],]$elected[]<-1
                       }

Resulting in these errors:
Error in `$<-.data.frame`(`*tmp*`, "marginwin", value = numeric(0)) :
replacement has 0 rows, data has 1

Any words of wisdom?

Thanks from Ghana!!
Julie


_______________________________________________
gov2001-l mailing list
gov2001-l@lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l



_______________________________________________
gov2001-l mailing list
gov2001-l@lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l