Julie,

Two things:

1) The big one (that's causing the error) is that you need to create the variable ahead of time.  If you put something like:  

data$marginwin <- NA
data$elected <- NA
outside of the loop then you will be fine.  Only when you are filling in a column with the right total number of rows does the adding it on the fly strategy work.

2) I can't tell from your code but it looks like you want the marginwin added to everyone in that election.  Your code won't do that.  It will just add it into the winner as written.  If you want that you need to fill in the 

[ ]

with something that has the length of candidates.  I'd go with 

nrow(ordered)

Brandon


On Tue, May 31, 2011 at 10:51 AM, Julie Faller <jfaller@fas.harvard.edu> wrote:
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



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