我正在处理一个大型数据集。我的目标是随着时间的推移获取特定国家的某些事件(如数据集中编码)的总和。数据集太大了,我必须使用函数按月加载它。数据来自 GDELT 数据集,可在此处获得:http ://gdelt.utdallas.edu/data/backfiles/ ?O=D 我已将 csv 转换为 Rdata,以便更快地读取和写入。它是一个包含 57 个不同变量的数据集。
# Create empty dataframes for all countries to later store data in.
Countries <- c("MAR","DZA","TUN","LBY","EGY","ISR",
"JOR","SYR","TUR","GEO","UKR","RUS","BLR")
loadNames <- function(CountryName) {
a <- data.frame()
assign(CountryName, a, pos = .GlobalEnv)
}
lapply(Countries,loadNames)
loadMonth <- function(MonthName) {
pb <- txtProgressBar(min = 0, max = total, initial = 0, char = "=", style = 1, width = 10)
# Load the month.
load(paste("/Users/mennoschellekens/Dropbox/HCSS-workinprogress/GDELT/Rdata/",MonthName,".RData", sep = ""), envir=environment())
colnames(Month) <- names(Header.57)
# Create a subset of relevant data for faster looping.
y <- subset(Month, ((Actor1CountryCode == "SYR" | Actor1CountryCode =="MAR" | Actor1CountryCode =="DZA" | Actor1CountryCode == "TUN" | Actor1CountryCode == "LBY" | Actor1CountryCode == "EGY" | Actor1CountryCode == "ISR" | Actor1CountryCode == "JOR" | Actor1CountryCode == "TUR" | Actor1CountryCode == "GEO" | Actor1CountryCode == "UKR" | Actor1CountryCode == "RUS" | Actor1CountryCode == "BLR") & (Actor2CountryCode == "SYR" | Actor2CountryCode == "MAR" | Actor2CountryCode == "DZA" | Actor2CountryCode == "TUN" | Actor2CountryCode == "LBY" | Actor2CountryCode == "EGY" | Actor2CountryCode == "ISR" | Actor2CountryCode == "JOR" | Actor2CountryCode == "TUR" | Actor2CountryCode == "GEO" | Actor2CountryCode == "UKR" | Actor2CountryCode == "RUS" | Actor2CountryCode == "BLR")))
#Define the events I want.
QuadCat <- c(1,2,3,4)
# Define the countries I want.
CountryString <- c("MAR","DZA", "TUN","LBY","EGY","ISR",
"JOR","SYR","TUR","GEO","UKR","RUS","BLR")
CountryData <- c(MAR,DZA,TUN,LBY,EGY,ISR,JOR,SYR,TUR,GEO,UKR,RUS,BLR)
# I want to check the above events for each country, using the function 'Check Events' with an embedded 'for loop'.
CheckEvents <- function(CountryData,CountryString) {
x <- subset(y, ((Actor1CountryCode == CountryString) & (Actor2CountryCode == CountryString)))
# This is the problem:
for (Y in QuadCat) {
e[[Y]] <- (sum(x$QuadClass == Y))
e <- rbind(CountryData,c(e))
assign(CountryString, as.data.frame(e), pos = .GlobalEnv)
}
}
mapply(CheckEvents, CountryData = CountryData, CountryString = CountryString)
} ###### END
第一次运行的输出给出了一个带有四个数字的向量,这很好,然后存储在 Global Environment 中。rbind()
但是,当我尝试使用,cbind()
或将该结果绑定到新结果时merge()
,它给了我非常奇怪的结果。最值得注意的是,它拒绝CountryData
作为向量读取,而只取向量中的最后一个值。我不明白它在做什么以及为什么它不会绑定。