Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #This code will provide functions that will read all of the GDELT data into R
- #It will do so without leaving large files on your disk
- #Its subsampling motives are, for now, limited to year and ActionGeo_CountryCode
- #The code below is written by Benjamin Lind under CC: BY-NC-SA 3.0
- #If you use or develop this code, please provide proper attribution.
- #You may contact me at lind.benjamin//at//gmail//d0t//com
- #You may also find me @benjaminlind on Twitter
- #For information on the GDELT project, see:
- #http://gdeltproject.org/
- #If you use data from this project, please cite it as:
- #Leetaru, Kalev and Schrodt, Philip. (2013). GDELT: Global Data on Events, Language, and Tone, 1979-2012. International Studies Association Annual Conference, April 2013. San Diego, CA.
- #To load these functions into R, uncomment the following four lines and enter them into R:
- #library(utils)
- #download.file("http://pastebin.com/raw.php?i=LrQhuVEZ", "Reading.GDELT.R")
- #source("Reading.GDELT.R")
- #file.remove("Reading.GDELT.R")
- library(utils)
- library(compiler)
- #All the files available for downloading, as of 1 September, 2013
- names.zips<-c("201303.zip", "201302.zip", "201301.zip", "201212.zip", "201211.zip", "201210.zip", "201209.zip", "201208.zip", "201207.zip", "201206.zip", "201205.zip", "201204.zip", "201203.zip", "201202.zip", "201201.zip", "201112.zip", "201111.zip", "201110.zip", "201109.zip", "201108.zip", "201107.zip", "201106.zip", "201105.zip", "201104.zip", "201103.zip", "201102.zip", "201101.zip", "201012.zip", "201011.zip", "201010.zip", "201009.zip", "201008.zip", "201007.zip", "201006.zip", "201005.zip", "201004.zip", "201003.zip", "201002.zip", "201001.zip", "200912.zip", "200911.zip", "200910.zip", "200909.zip", "200908.zip", "200907.zip", "200906.zip", "200905.zip", "200904.zip", "200903.zip", "200902.zip", "200901.zip", "200812.zip", "200811.zip", "200810.zip", "200809.zip", "200808.zip", "200807.zip", "200806.zip", "200805.zip", "200804.zip", "200803.zip", "200802.zip", "200801.zip", "200712.zip", "200711.zip", "200710.zip", "200709.zip", "200708.zip", "200707.zip", "200706.zip", "200705.zip", "200704.zip", "200703.zip", "200702.zip", "200701.zip", "200612.zip", "200611.zip", "200610.zip", "200609.zip", "200608.zip", "200607.zip", "200606.zip", "200605.zip", "200604.zip", "200603.zip", "200602.zip", "200601.zip", "2005.zip", "2004.zip", "2003.zip", "2002.zip", "2001.zip", "2000.zip", "1999.zip", "1998.zip", "1997.zip", "1996.zip", "1995.zip", "1994.zip", "1993.zip", "1992.zip", "1991.zip", "1990.zip", "1989.zip", "1988.zip", "1987.zip", "1986.zip", "1985.zip", "1984.zip", "1983.zip", "1982.zip", "1981.zip", "1980.zip", "1979.zip")
- url.root.dir<-"http://gdeltproject.org/data/backfiles/" #Parent directory
- names.zips.url<-paste(url.root.dir, names.zips, sep="") #Full file address
- names.year<-as.numeric(strtrim(names.zips, 4)) #Year of each file
- gdelt.full.files<-data.frame(file=names.zips, url=names.zips.url, year=names.year, stringsAsFactors=FALSE) #Pull together into a data frame
- rm(names.zips, names.zips.url, names.year, url.root.dir) #Clean-up
- #Do the same for the Daily Updates
- daily.names<-seq.Date(from=as.Date("20130401", "%Y%m%d"), to=as.Date(date(), "%a %b %d %H:%M:%S %Y")-1, by=1) #From 1 April, 2013 to yesterday
- daily.names.date<-as.character(format.Date(daily.names, "%Y%m%d"))
- daily.names.files<-paste(gsub("-", "", daily.names), ".export.CSV.zip", sep="")
- url.root.dir<-"http://gdeltproject.org/data/dailyupdates/" #Parent directory
- daily.names.zips.url<-paste(url.root.dir, daily.names.files, sep="") #Full file address
- gdelt.daily.files<-data.frame(file=daily.names.files, url=daily.names.zips.url, date=daily.names.date, stringsAsFactors=FALSE)
- rm(daily.names, daily.names.files, daily.names.zips.url, daily.names.date, url.root.dir)
- #These next lines are to download the information on the CAMEO codes.
- #See: http://gdeltproject.org/data/documentation/CAMEO.Manual.1.1b3.pdf
- read.sstab<-function(theurl, ...){ #Function to read in a data table from online
- #_theurl_ refers to the location of the data
- #_..._ are parameters passed onto read.table
- Sys.sleep(2) #Two second delay.
- file.name<-unlist(strsplit(theurl, "/")) #Splits the URL
- file.name<-file.name[length(file.name)] #The file name as presented in the URL
- download.file(theurl, file.name)
- outtab<-read.table(file.name, sep="\t", ...)
- file.remove(file.name)
- return(outtab)
- }
- read.sstab<-cmpfun(read.sstab)
- CAMEO.codes<-c(country="CAMEO.country.txt", group="CAMEO.knowngroup.txt", ethnic="CAMEO.ethnic.txt", religion="CAMEO.religion.txt", type="CAMEO.type.txt", event="CAMEO.eventcodes.txt", goldstein="CAMEO.goldsteinscale.txt") #These are the file names
- CAMEO.codes<-data.frame(file.name=CAMEO.codes, file.url=paste("http://gdeltproject.org/data/lookups/", CAMEO.codes, sep=""), stringsAsFactors=FALSE) #Join with URL
- CAMEO.codebooks<-lapply(CAMEO.codes$file.url, read.sstab, quote="", header=TRUE, as.is=TRUE, stringsAsFactors=FALSE, colClasses=c("character", "character")) #Download the tables
- names(CAMEO.codebooks)<-rownames(CAMEO.codes) #Name the list
- #Aside from the Goldstein Scale, all items should be characters.
- CAMEO.codebooks$goldstein$GOLDSTEINSCALE<-as.numeric(CAMEO.codebooks$goldstein$GOLDSTEINSCALE)
- CAMEO.codes<-list(codebooks=CAMEO.codebooks, source=CAMEO.codes) #Combine
- rm(CAMEO.codebooks, read.sstab) #Clean-up
- read.gdelt<-function(file.name, daily=FALSE, ActionGeo_CountryCode=NULL){
- #_file.name_ is the name of the .csv file extracted from the corresponding .zip
- #_daily_, if set to TRUE, indicates that file.name comes from the Daily Updates data source
- #Filters to subsample
- #_ActionGeo_CountryCode_: Character vector.
- #Codebook advises to use this variable for location filtering
- #Description: "This is the 2-character FIPS10-4 country code for the location."
- #For the codes, see the PDF/XLS here: http://earth-info.nga.mil/gns/html/gazetteers2.html
- #Other filtering variables can be added. For now, I'm only interested in _ActionGeo_CountryCode_
- #Column names come from "http://gdeltproject.org/data/lookups/CSV.header.dailyupdates.txt"
- #I suppose names below could be automatically downloaded from that site, but whatevs
- #Disadvantages: 1) The code looks less clean and 2) constants are introduced
- #Advantages: 1) The column names are listed before us, making subsampling easier, and 2) these constants will be introduced anyways due to the colClasses argument.
- out.dat.colnames<-c("GLOBALEVENTID", "SQLDATE", "MonthYear", "Year", "FractionDate", "Actor1Code", "Actor1Name", "Actor1CountryCode", "Actor1KnownGroupCode", "Actor1EthnicCode", "Actor1Religion1Code", "Actor1Religion2Code", "Actor1Type1Code", "Actor1Type2Code", "Actor1Type3Code", "Actor2Code", "Actor2Name", "Actor2CountryCode", "Actor2KnownGroupCode", "Actor2EthnicCode", "Actor2Religion1Code", "Actor2Religion2Code", "Actor2Type1Code", "Actor2Type2Code", "Actor2Type3Code", "IsRootEvent", "EventCode", "EventBaseCode", "EventRootCode", "QuadClass", "GoldsteinScale", "NumMentions", "NumSources", "NumArticles", "AvgTone", "Actor1Geo_Type", "Actor1Geo_FullName", "Actor1Geo_CountryCode", "Actor1Geo_ADM1Code", "Actor1Geo_Lat", "Actor1Geo_Long", "Actor1Geo_FeatureID", "Actor2Geo_Type", "Actor2Geo_FullName", "Actor2Geo_CountryCode", "Actor2Geo_ADM1Code", "Actor2Geo_Lat", "Actor2Geo_Long", "Actor2Geo_FeatureID", "ActionGeo_Type", "ActionGeo_FullName", "ActionGeo_CountryCode", "ActionGeo_ADM1Code", "ActionGeo_Lat", "ActionGeo_Long", "ActionGeo_FeatureID", "DATEADDED", "SOURCEURL")
- #Setting the column classes is important here. R would otherwise autodetect many of the character fields as integer/numerics (because they're expressed as numbers). Setting these columns as a character class is important when it comes to matching their verbal descriptions.
- out.dat.colClasses<-c(GLOBALEVENTID="integer", SQLDATE="integer", MonthYear="integer", Year="integer", FractionDate="numeric", Actor1Code="character", Actor1Name="character", Actor1CountryCode="character", Actor1KnownGroupCode="character", Actor1EthnicCode="character", Actor1Religion1Code="character", Actor1Religion2Code="character", Actor1Type1Code="character", Actor1Type2Code="character", Actor1Type3Code="character", Actor2Code="character", Actor2Name="character", Actor2CountryCode="character", Actor2KnownGroupCode="character", Actor2EthnicCode="character", Actor2Religion1Code="character", Actor2Religion2Code="character", Actor2Type1Code="character", Actor2Type2Code="character", Actor2Type3Code="character", IsRootEvent="integer", EventCode="character", EventBaseCode="character", EventRootCode="character", QuadClass="integer", GoldsteinScale="numeric", NumMentions="integer", NumSources="integer", NumArticles="integer", AvgTone="numeric", Actor1Geo_Type="integer", Actor1Geo_FullName="character", Actor1Geo_CountryCode="character", Actor1Geo_ADM1Code="character", Actor1Geo_Lat="numeric", Actor1Geo_Long="numeric", Actor1Geo_FeatureID="integer", Actor2Geo_Type="integer", Actor2Geo_FullName="character", Actor2Geo_CountryCode="character", Actor2Geo_ADM1Code="character", Actor2Geo_Lat="numeric", Actor2Geo_Long="numeric", Actor2Geo_FeatureID="integer", ActionGeo_Type="integer", ActionGeo_FullName="character", ActionGeo_CountryCode="character", ActionGeo_ADM1Code="character", ActionGeo_Lat="numeric", ActionGeo_Long="numeric", ActionGeo_FeatureID="integer", DATEADDED="integer", SOURCEURL="character")
- if (daily){
- #Some of the FeatureIDs in the daily updates are characters. Same for IsRootEvent, among others.
- #If other problems arise with an error that reads something like, "Expecting an integer, got <a character>", identify the offending column, add its name below, then add another "suppressWarnings" line.
- out.dat.colClasses[c("Actor1Geo_FeatureID", "Actor2Geo_FeatureID", "ActionGeo_FeatureID", "IsRootEvent", "GoldsteinScale", "NumMentions", "AvgTone", "NumSources", "NumArticles", "Actor1Geo_Lat", "Actor1Geo_Long", "Actor2Geo_Lat", "Actor2Geo_Long", "ActionGeo_Lat", "ActionGeo_Long")]<-"character"
- out.dat<-read.table(file.name, header=FALSE, sep="\t", quote="\"", col.names=out.dat.colnames, colClasses=out.dat.colClasses, na.strings=c("NA", ""), as.is=TRUE, stringsAsFactors=FALSE, comment.char="")
- #Reset the FeatureIDs to integers, etc.
- #Will issue a warning as some of the characters do not correspond to integers.
- #Those will receive NA values.
- suppressWarnings(out.dat$Actor1Geo_FeatureID<-as.integer(out.dat$Actor1Geo_FeatureID))
- suppressWarnings(out.dat$Actor2Geo_FeatureID<-as.integer(out.dat$Actor2Geo_FeatureID))
- suppressWarnings(out.dat$ActionGeo_FeatureID<-as.integer(out.dat$ActionGeo_FeatureID))
- suppressWarnings(out.dat$IsRootEvent<-as.integer(out.dat$IsRootEvent))
- suppressWarnings(out.dat$GoldsteinScale<-as.numeric(out.dat$GoldsteinScale))
- suppressWarnings(out.dat$NumMentions<-as.integer(out.dat$NumMentions))
- suppressWarnings(out.dat$AvgTone<-as.numeric(out.dat$AvgTone))
- suppressWarnings(out.dat$NumSources<-as.integer(out.dat$NumSources))
- suppressWarnings(out.dat$NumArticles<-as.integer(out.dat$NumArticles))
- suppressWarnings(out.dat$Actor1Geo_Lat<-as.numeric(out.dat$Actor1Geo_Lat))
- suppressWarnings(out.dat$Actor1Geo_Long<-as.numeric(out.dat$Actor1Geo_Long))
- suppressWarnings(out.dat$Actor2Geo_Lat<-as.numeric(out.dat$Actor2Geo_Lat))
- suppressWarnings(out.dat$Actor2Geo_Long<-as.numeric(out.dat$Actor2Geo_Long))
- suppressWarnings(out.dat$ActionGeo_Lat<-as.numeric(out.dat$ActionGeo_Lat))
- suppressWarnings(out.dat$ActionGeo_Long<-as.numeric(out.dat$ActionGeo_Long))
- }
- else #SOURCEURL is only available for the daily updates
- out.dat<-read.table(file.name, header=FALSE, sep="\t", quote="\"", col.names=out.dat.colnames[1:(length(out.dat.colnames)-1)], colClasses=out.dat.colClasses[1:(length(out.dat.colnames)-1)], na.strings=c("NA", ""), as.is=TRUE, stringsAsFactors=FALSE, comment.char="")
- rownames(out.dat)<-out.dat$GLOBALEVENTID
- out.dat$IsRootEvent<-as.logical(out.dat$IsRootEvent) #Original data in 0s and 1s
- #Some cleaning. Values that don't belong get NA
- #Country codes
- if (any(out.dat$Actor1CountryCode%in%CAMEO.codes$codebooks$country$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor1CountryCode%in%CAMEO.codes$codebooks$country$CODE==FALSE)
- out.dat$Actor1CountryCode[bad.cases]<-NA
- }
- if (any(out.dat$Actor2CountryCode%in%CAMEO.codes$codebooks$country$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor2CountryCode%in%CAMEO.codes$codebooks$country$CODE==FALSE)
- out.dat$Actor2CountryCode[bad.cases]<-NA
- }
- #Known group codes
- if (any(out.dat$Actor1KnownGroupCode%in%CAMEO.codes$codebooks$group$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor1KnownGroupCode%in%CAMEO.codes$codebooks$group$CODE==FALSE)
- out.dat$Actor1KnownGroupCode[bad.cases]<-NA
- }
- if (any(out.dat$Actor2KnownGroupCode%in%CAMEO.codes$codebooks$group$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor2KnownGroupCode%in%CAMEO.codes$codebooks$group$CODE==FALSE)
- out.dat$Actor2KnownGroupCode[bad.cases]<-NA
- }
- #Ethnic codes
- if (any(out.dat$Actor1EthnicCode%in%CAMEO.codes$codebooks$ethnic$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor1EthnicCode%in%CAMEO.codes$codebooks$ethnic$CODE==FALSE)
- out.dat$Actor1EthnicCode[bad.cases]<-NA
- }
- if (any(out.dat$Actor2EthnicCode%in%CAMEO.codes$codebooks$ethnic$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor2EthnicCode%in%CAMEO.codes$codebooks$ethnic$CODE==FALSE)
- out.dat$Actor2EthnicCode[bad.cases]<-NA
- }
- #Religion codes
- if (any(out.dat$Actor1Religion1Code%in%CAMEO.codes$codebooks$religion$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor1Religion1Code%in%CAMEO.codes$codebooks$religion$CODE==FALSE)
- out.dat$Actor1Religion1Code[bad.cases]<-NA
- }
- if (any(out.dat$Actor1Religion2Code%in%CAMEO.codes$codebooks$religion$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor1Religion2Code%in%CAMEO.codes$codebooks$religion$CODE==FALSE)
- out.dat$Actor1Religion2Code[bad.cases]<-NA
- }
- if (any(out.dat$Actor2Religion1Code%in%CAMEO.codes$codebooks$religion$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor2Religion1Code%in%CAMEO.codes$codebooks$religion$CODE==FALSE)
- out.dat$Actor2Religion1Code[bad.cases]<-NA
- }
- if (any(out.dat$Actor2Religion2Code%in%CAMEO.codes$codebooks$religion$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor2Religion2Code%in%CAMEO.codes$codebooks$religion$CODE==FALSE)
- out.dat$Actor2Religion2Code[bad.cases]<-NA
- }
- #Type codes
- if (any(out.dat$Actor1Type1Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor1Type1Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)
- out.dat$Actor1Type1Code[bad.cases]<-NA
- }
- if (any(out.dat$Actor1Type2Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor1Type2Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)
- out.dat$Actor1Type2Code[bad.cases]<-NA
- }
- if (any(out.dat$Actor1Type3Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor1Type3Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)
- out.dat$Actor1Type3Code[bad.cases]<-NA
- }
- if (any(out.dat$Actor2Type1Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor2Type1Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)
- out.dat$Actor2Type1Code[bad.cases]<-NA
- }
- if (any(out.dat$Actor2Type2Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor2Type2Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)
- out.dat$Actor2Type2Code[bad.cases]<-NA
- }
- if (any(out.dat$Actor2Type3Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)){
- bad.cases<-which(out.dat$Actor2Type3Code%in%CAMEO.codes$codebooks$type$CODE==FALSE)
- out.dat$Actor2Type3Code[bad.cases]<-NA
- }
- #Event codes
- if (any(out.dat$EventCode%in%CAMEO.codes$codebooks$event$CAMEOEVENTCODE==FALSE)){
- bad.cases<-which(out.dat$EventCode%in%CAMEO.codes$codebooks$event$CAMEOEVENTCODE==FALSE)
- out.dat$EventCode[bad.cases]<-NA
- }
- if (any(out.dat$EventBaseCode%in%CAMEO.codes$codebooks$event$CAMEOEVENTCODE==FALSE)){
- bad.cases<-which(out.dat$EventBaseCode%in%CAMEO.codes$codebooks$event$CAMEOEVENTCODE==FALSE)
- out.dat$EventBaseCode[bad.cases]<-NA
- }
- if (any(out.dat$EventRootCode%in%CAMEO.codes$codebooks$event$CAMEOEVENTCODE==FALSE)){
- bad.cases<-which(out.dat$EventRootCode%in%CAMEO.codes$codebooks$event$CAMEOEVENTCODE==FALSE)
- out.dat$EventRootCode[bad.cases]<-NA
- }
- #Here's where the data should be subsampled
- if (is.null(ActionGeo_CountryCode)==FALSE){
- country.filter<-which(out.dat$ActionGeo_CountryCode%in%ActionGeo_CountryCode)
- if (length(country.filter)==0){ #In case none of the events satisfy the filter
- out.dat<-out.dat[1,] #Preserve the format
- out.dat[,]<-NA #Assign all NA
- cat("\n None of the cases in '", file.name, "' passed through the filter. Returning NA.\n", sep="")
- }
- else
- out.dat<-out.dat[country.filter,]
- }
- #Add other filters, if needed, here
- return(out.dat)
- }
- #read.gdelt() will be greedy on the resources. Hopefully better performance if compiled.
- read.gdelt<-cmpfun(read.gdelt)
- dluzrd<-function(file.url, ...){
- #DownLoad UnZip Read Delete
- #_file.url_ should correspond to gdelt.full.files$url[x]
- #_..._ are parameters to pass on to the read command
- file.name<-unlist(strsplit(file.url, "/")) #Splits the URL
- file.name<-file.name[length(file.name)] #The file name as presented in the URL
- download.file(file.url, file.name) #The files are big, so this could take a few minutes
- unzip(file.name) #Extract the contents
- #Hopefully all of the .zip files will always and forever contain only one file, and that file is the .csv
- file.csv<-unzip(file.name, list=TRUE)[1,"Name"] #Create .csv file name
- file.csv<-toString(file.csv)
- out.dat<-read.gdelt(file.csv, ...)
- file.remove(file.name, file.csv)
- return(out.dat)
- }
- #To avoid running a for loop in R
- dluzrd<-Vectorize(dluzrd, vectorize.args="file.url", SIMPLIFY=FALSE)
- dluzrd<-cmpfun(dluzrd)
- #All of the above functions could be nested within this next one, but the code reads cleaner this way. That, and some of them could be individually useful.
- acquire.gdelt<-function(years.interest=c(1979:2013), country.code=NULL){
- #_years.interest_ are the years you want to sample
- #_country.code_ is a vector of FIPS10-4 country codes for the location
- #By default, this program select all years and all countries
- #If you value your RAM and download time, I would not recommend using the defaults on most computers
- years.interest<-which(gdelt.full.files$year%in%years.interest) #Return an index
- gdelt.interest<-dluzrd(gdelt.full.files$url[years.interest], daily=FALSE, ActionGeo_CountryCode=country.code) #This function will return a list
- gdelt.interest<-do.call(rbind, gdelt.interest) #Turn this list into a single dataframe
- rownames(gdelt.interest)<-1:nrow(gdelt.interest)
- return(gdelt.interest)
- }
- acquire.gdelt<-cmpfun(acquire.gdelt) #Compiling might help
- acquire.gdelt.daily<-function(start.date=20130401, end.date=as.character(format.Date(as.Date(date(), "%a %b %d %H:%M:%S %Y")-1, "%Y%m%d")), country.code=NULL){
- #_start.date_ is the date, expressed as the number YYYYMMDD, of when to start retrieving data
- #_end.date_ is the date, expressed as the number YYYYMMDD, of when to end retrieving data
- #By default, _end.date_ is yesterday.
- #_country.code_ is a vector of FIPS10-4 country codes for the location
- #By default, this program select all years and all countries
- #If you value your RAM and download time, I would not recommend using the defaults on most computers
- dates.to.retrieve<-seq.Date(from=as.Date(as.character(start.date), "%Y%m%d"), to=as.Date(as.character(end.date), "%Y%m%d"), by=1)
- dates.to.retrieve<-as.character(format.Date(dates.to.retrieve, "%Y%m%d"))
- dates.to.retrieve<-which(gdelt.daily.files$date%in%dates.to.retrieve) #Return an index
- gdelt.interest<-dluzrd(gdelt.daily.files$url[dates.to.retrieve], daily=TRUE, ActionGeo_CountryCode=country.code) #This function will return a list
- gdelt.interest<-do.call(rbind, gdelt.interest) #Turn this list into a single dataframe
- rownames(gdelt.interest)<-1:nrow(gdelt.interest)
- return(gdelt.interest)
- }
- acquire.gdelt.daily<-cmpfun(acquire.gdelt.daily) #Compiling might help
- assign.CAMEO<-function(your.gdelt){
- #_your.gdelt_ should be dataset produced by acquire.gdelt()
- #This function will create verbal descriptions for the CAMEO codes in _your.gdelt_
- #These tables will likely be resource needy.
- Actor1CountryCode.verbal<-CAMEO.codes$codebooks$country$LABEL[match(your.gdelt$Actor1CountryCode, CAMEO.codes$codebooks$country$CODE)]
- Actor2CountryCode.verbal<-CAMEO.codes$codebooks$country$LABEL[match(your.gdelt$Actor2CountryCode, CAMEO.codes$codebooks$country$CODE)]
- Actor1KnownGroupCode.verbal<-CAMEO.codes$codebooks$group$LABEL[match(your.gdelt$Actor1KnownGroupCode, CAMEO.codes$codebooks$group$CODE)]
- Actor2KnownGroupCode.verbal<-CAMEO.codes$codebooks$group$LABEL[match(your.gdelt$Actor2KnownGroupCode, CAMEO.codes$codebooks$group$CODE)]
- Actor1EthnicCode.verbal<-CAMEO.codes$codebooks$ethnic$LABEL[match(your.gdelt$Actor1EthnicCode, CAMEO.codes$codebooks$ethnic$CODE)]
- Actor2EthnicCode.verbal<-CAMEO.codes$codebooks$ethnic$LABEL[match(your.gdelt$Actor2EthnicCode, CAMEO.codes$codebooks$ethnic$CODE)]
- Actor1Religion1Code.verbal<-CAMEO.codes$codebooks$religion$LABEL[match(your.gdelt$Actor1Religion1Code, CAMEO.codes$codebooks$religion$CODE)]
- Actor1Religion2Code.verbal<-CAMEO.codes$codebooks$religion$LABEL[match(your.gdelt$Actor1Religion2Code, CAMEO.codes$codebooks$religion$CODE)]
- Actor2Religion1Code.verbal<-CAMEO.codes$codebooks$religion$LABEL[match(your.gdelt$Actor2Religion1Code, CAMEO.codes$codebooks$religion$CODE)]
- Actor2Religion2Code.verbal<-CAMEO.codes$codebooks$religion$LABEL[match(your.gdelt$Actor2Religion2Code, CAMEO.codes$codebooks$religion$CODE)]
- Actor1Type1Code.verbal<-CAMEO.codes$codebooks$type$LABEL[match(your.gdelt$Actor1Type1Code, CAMEO.codes$codebooks$type$CODE)]
- Actor1Type2Code.verbal<-CAMEO.codes$codebooks$type$LABEL[match(your.gdelt$Actor1Type2Code, CAMEO.codes$codebooks$type$CODE)]
- Actor1Type3Code.verbal<-CAMEO.codes$codebooks$type$LABEL[match(your.gdelt$Actor1Type3Code, CAMEO.codes$codebooks$type$CODE)]
- Actor2Type1Code.verbal<-CAMEO.codes$codebooks$type$LABEL[match(your.gdelt$Actor2Type1Code, CAMEO.codes$codebooks$type$CODE)]
- Actor2Type2Code.verbal<-CAMEO.codes$codebooks$type$LABEL[match(your.gdelt$Actor2Type2Code, CAMEO.codes$codebooks$type$CODE)]
- Actor2Type3Code.verbal<-CAMEO.codes$codebooks$type$LABEL[match(your.gdelt$Actor2Type3Code, CAMEO.codes$codebooks$type$CODE)]
- EventCode.verbal<-CAMEO.codes$codebooks$event$EVENTDESCRIPTION[match(your.gdelt$EventCode, CAMEO.codes$codebooks$event$CAMEOEVENTCODE)]
- EventBaseCode.verbal<-CAMEO.codes$codebooks$event$EVENTDESCRIPTION[match(your.gdelt$EventBaseCode, CAMEO.codes$codebooks$event$CAMEOEVENTCODE)]
- EventRootCode.verbal<-CAMEO.codes$codebooks$event$EVENTDESCRIPTION[match(your.gdelt$EventRootCode, CAMEO.codes$codebooks$event$CAMEOEVENTCODE)]
- QuadClass.vec<-c("Verbal Cooperation", "Material Cooperation", "Verbal Conflict", "Material Conflict")
- QuadClass.verbal<-QuadClass.vec[your.gdelt$QuadClass]
- Geo_Type.vec<-c(NA, "COUNTRY", "USSTATE", "USCITY", "WORLDCITY", "WORLDSTATE")
- Actor1Geo_Type.verbal<-Geo_Type.vec[your.gdelt$Actor1Geo_Type+1]
- Actor2Geo_Type.verbal<-Geo_Type.vec[your.gdelt$Actor2Geo_Type+1]
- ActionGeo_Type.verbal<-Geo_Type.vec[your.gdelt$ActionGeo_Type+1]
- #Goldstein Scale already assigned in the data
- out.df<-data.frame(Actor1CountryCode.verbal, Actor2CountryCode.verbal, Actor1KnownGroupCode.verbal, Actor2KnownGroupCode.verbal, Actor1EthnicCode.verbal, Actor2EthnicCode.verbal, Actor1Religion1Code.verbal, Actor1Religion2Code.verbal, Actor2Religion1Code.verbal, Actor2Religion2Code.verbal, Actor1Type1Code.verbal, Actor1Type2Code.verbal, Actor1Type3Code.verbal, Actor2Type1Code.verbal, Actor2Type2Code.verbal, Actor2Type3Code.verbal, EventCode.verbal, EventBaseCode.verbal, EventRootCode.verbal, QuadClass.verbal, Actor1Geo_Type.verbal, Actor2Geo_Type.verbal, ActionGeo_Type.verbal, stringsAsFactors=FALSE)
- return(out.df)
- }; assign.CAMEO<-cmpfun(assign.CAMEO)
Advertisement
Add Comment
Please, Sign In to add comment