0

I am working with the GTD database- can be downloaded at (http://www.start.umd.edu/gtd/contact/)

I have a subset of the data only for the United States, and what I am trying to do is add information on if the political party in power is Republican or Democrat. The years I am using come from http://candychang.com/?p=4346

My very basic knowledge of R has lead me to this,

gtd<- read.csv("gtd.csv")
US <- subset(x=gtd, country_txt== "United States")
Republican <- subset(x=US, iyear %in% c(1971:1976, 1981:1992, 2001:2008))
Democrat <- subset(x=US, iyear %in% c(1977:1980, 1993:2000, 2009:2011))
install.packages("plyr")
require(plyr)
republicanattacks <- ddply(Republican, .(iyear), "nrow")
democratattacks<- ddply(Democrat, .(iyear), "nrow")
transform(republicanattacks, new="Republican")
transform(democratattacks, new="Democrat")
rbind(republicanattacks,democratattacks) #does not work- Error in match.names(clabs, names(xi)) : 

names do not match previous names

I want to basically just stick these data frames together so I can make one graph but I haven't been able to do that. Ultimately my goal is to display the number of attacks for republican/democrats graphically to show the comparison, and I don't want to do this on separate graphs- I am sure there is an easy way to merge these but the ways I've tried haven't work. I was also wondering if there is a more simple way to do this starting with the US subset like with a For loop or something, I am just very unfamiliar with for loops. But basically I just need to know how to merge these..

Jaap
  • 81,064
  • 34
  • 182
  • 193
hk47
  • 127
  • 3
  • 14
  • you should use the dplyr package instead, it will be much faster. Would `merge` be more useful instead of `rbind`? – JeremyS Mar 27 '14 at 05:27
  • Not really sure the data structure, but can you add a categorical variable (factor) that is used to distinguish the color of the data points. You could set it to `Republican` for all years initially, and then use `which` where you have `subset` to assign `Democrat` to those certain years. – beroe Mar 27 '14 at 08:27
  • You are much more likely to receive a helpful answer if you post a tiny, easily copy-paste-able toy data set that people can play with. Right now, I think many people are reluctant to sign up on a web-page to be able to access your data. Also post your desired output. Please check these links for general ideas, and how to do it in R: [**here**](http://stackoverflow.com/help/mcve), [**here**](http://www.sscce.org/), and [**here**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – Henrik Mar 27 '14 at 08:49

1 Answers1

0

Try

library(plyr)

rbind.fill(republicanattacks,democratattacks)

hope it helps...

Miha Trošt
  • 2,002
  • 22
  • 25