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(a)lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l