-1

我正在处理一个大型数据集。我的目标是随着时间的推移获取特定国家的某些事件(如数据集中编码)的总和。数据集太大了,我必须使用函数按月加载它。数据来自 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作为向量读取,而只取向量中的最后一个值。我不明白它在做什么以及为什么它不会绑定。

4

1 回答 1

0

我认为以下内容会为您提供所需的列表。然后,您可以随心所欲地重塑它。但是,您的示例代码非常令人困惑,并且涉及一些潜在的危险元素,例如 usingassignsubsetinside 函数。您似乎还有很多多余的代码。我想所有这些都是导致你出现问题的原因。

Countries <- c("MAR","DZA","TUN","LBY","EGY","ISR",
               "JOR","SYR","TUR","GEO","UKR","RUS","BLR")

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)

  y <- Month[(Month$Actor1CountryCode %in% Countries) &
             (Month$Actor2CountryCode %in% Countries),] # subset
  CheckEvents <- function(CountryString) {
    x <- y[(y$Actor1CountryCode == CountryString) &
           (y$Actor2CountryCode == CountryString),]
    # 1:4 below was your values of QuadCat
    sapply(1:4, function(Y) sum(x$QuadClass == Y)) # should return a vector
  }
  # build list of vectors from `CheckEvents`, one for each country
  out <- lapply(Countries, CheckEvents)
  names(out) <- Countries
  return(out)
}
loadMonth("January") # get the list for one month; not sure how you have months named
于 2013-07-26T14:48:43.427 回答