Julie,
Good point. Sorry about that. The data just looks like this:> head(data)region constituency party year name vote1 ASHANTI ADANSI ASOKWA CPP 2008 OWUSU-BOAMAH FRANCIS 3142 ASHANTI ADANSI ASOKWA DFP 2008 AMOAKO ANAAFI 13323 ASHANTI ADANSI ASOKWA NDC 2008 ALHAJI ABDUL-LATEEF MADJOUB 92194 ASHANTI ADANSI ASOKWA NPP 2008 KOBINA TAHIR HAMMON 136595 ASHANTI AFIGYA-SEKYERE EAST CPP 2008 OBENG NYANTAKYI CLEMENT 6406 ASHANTI AFIGYA-SEKYERE EAST DPP 2008 AMIDU ALHAJI ADAM 127source rejected1 EC pdf from http://www.ec.gov.gh/node/208 4522 EC pdf from http://www.ec.gov.gh/node/208 4523 EC pdf from http://www.ec.gov.gh/node/208 4524 EC pdf from http://www.ec.gov.gh/node/208 4525 EC pdf from http://www.ec.gov.gh/node/208 3346 EC pdf from http://www.ec.gov.gh/node/208 334On 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.BrandonOn 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