【问题标题】:Loss of attributes despite attempts to preserve them尽管尝试保留属性,但仍丢失属性
【发布时间】:2014-07-22 08:46:54
【问题描述】:

make 总是重建 Makefile 目标 (make always rebuilds Makefile targets) 的问题及其调查发现了另一个问题,这是本问题的主题。重复执行以下R 代码会导致在数据转换操作期间丢失对象的属性

为了记录,我不得不说我已经写过这个主题(Approaches to preserving object's attributes during extract/replace operations),但是那个问题和答案更笼统(而且我不正确地认为简单的保存属性有效 - 它对我有用写这篇文章的时候,因为当时我还没有执行操作,这对对象的属性有潜在的危险)。

以下是我的 R 代码的摘录,其中我遇到了属性丢失的问题。

##### GENERIC TRANSFORMATION FUNCTION #####

transformResult <- function (dataSource, indicator, handler) {

  fileDigest <- base64(indicator)
  rdataFile <- paste0(CACHE_DIR, "/", dataSource, "/",
                      fileDigest, RDS_EXT)
  if (file.exists(rdataFile)) {
    data <- readRDS(rdataFile)

    # Preserve user-defined attributes for data frame's columns
    # via defining new class 'avector' (see code below)). Also,
    # preserve attributes (comments) for the data frame itself.
    data2 <- data.frame(lapply(data, function(x) 
      { structure(x, class = c("avector", class(x))) } ))
    #mostattributes(data2) <- attributes(data)
    attributes(data2) <- attributes(data)

    result <- do.call(handler, list(indicator, data2))
    saveRDS(result, rdataFile)
    rm(result)
  }
  else {
    error("RDS file for \'", indicator, "\' not found! Run 'make' first.")
  }
}


## Preserve object's special attributes:
## use a class with a "as.data.frame" and "[" method

as.data.frame.avector <- as.data.frame.vector

`[.avector` <- function (x, ...) {
  #attr <- attributes(x)
  r <- NextMethod("[")
  mostattributes(r) <- attributes(x)
  #attributes(r) <- attr
  return (r)
}

##### HANDLER FUNCTION DEFINITIONS #####

projectAge <- function (indicator, data) {

  # do not process, if target column already exists
  if ("Project Age" %in% names(data)) {
    message("Project Age: ", appendLF = FALSE)
    message("Not processing - Transformation already performed!\n")
    return (invisible())
  }

  transformColumn <- as.numeric(unlist(data["Registration Time"]))
  regTime <- as.POSIXct(transformColumn, origin="1970-01-01")
  prjAge <- difftime(Sys.Date(), as.Date(regTime), units = "weeks")
  data[["Project Age"]] <- as.numeric(round(prjAge)) / 4 # in months

  # now we can delete the source column
  if ("Registration Time" %in% names(data))
    data <- data[setdiff(names(data), "Registration Time")]

    if (DEBUG2) {print(summary(data)); print("")}

  return (data)
}


projectLicense <- function (indicator, data) {

  # do not process, if target column (type) already exists
  if (is.factor(data[["Project License"]])) {
    message("Project License: ", appendLF = FALSE)
    message("Not processing - Transformation already performed!\n")
    return (invisible())
  }

  data[["Project License"]] <- 
    factor(data[["Project License"]],
           levels = c('gpl', 'lgpl', 'bsd', 'other',
                      'artistic', 'public', '(Other)'),
           labels = c('GPL', 'LGPL', 'BSD', 'Other',
                      'Artistic', 'Public', 'Unknown'))

  if (DEBUG2) {print(summary(data)); print("")}

  return (data)
}


devTeamSize <- function (indicator, data) {

  var <- data[["Development Team Size"]]

  # convert data type from 'character' to 'numeric' 
  if (!is.numeric(var)) {
    data[["Development Team Size"]] <- as.numeric(var)
  }

  if (DEBUG2) {print(summary(data)); print("")}

  return (data)
}


##### MAIN #####

# construct list of indicators & corresponding transform. functions
indicators <- c("prjAge", "prjLicense", "devTeamSize")
transforms <- list(projectAge, projectLicense, devTeamSize)

# sequentially call all previously defined transformation functions
lapply(seq_along(indicators),
       function(i) {
         transformResult("SourceForge",
                         indicators[[i]], transforms[[i]])
         })

第二次运行此代码后,名称“Project Age”和“Project License”以及数据框data2的其他用户定义属性丢失。

我的问题是多方面的:

1) 我的代码中的哪些语句可能导致属性丢失以及为什么;

2) transformResult()avector 类定义中的正确行代码(mostattributes &lt;- attributesattributes &lt;- attributes/attr)是什么以及为什么;

3) 是真正需要的语句 as.data.frame.avector &lt;- as.data.frame.vector,如果我将类属性 avector 添加到数据框对象,并且通常更喜欢通用解决方案(不仅适用于数据框);为什么或为什么不。

4) 通过attr在类定义中保存不起作用,它失败并出现以下错误:

Error in attributes(r) <- attr :
  'names' attribute [5] must be the same length as the vector [3]
Calls: lapply ... summary.data.frame -> lapply -> FUN -> summary.default -> [ -> [.avector

所以,我不得不重新使用mostattributes()。可以吗?

===========

我已阅读有关该主题的以下内容:

  1. SO 问题:How to delete a row from a data.frame without losing the attributes(我喜欢 Ben Barns 的解决方案,但它与 Gabor Grothendieck 和 Marc Schwartz 建议的解决方案有点不同 - 见下文);

  2. SO 问题:indexing operation removes attributes(虽然解决方案清晰易读,但我更喜欢一个,基于类定义 /sub-classing?/);

  3. Heinz Tuechler (https://stat.ethz.ch/pipermail/r-help/2006-July/109148.html) 建议的通用解决方案 - 我需要这个吗?;

  4. Brian Ripley (http://r.789695.n4.nabble.com/Losing-attributes-in-data-frame-PR-10873-tp919265p919266.html) 的解释 - 我觉得有点混乱;

  5. Gabor Grothendieck (https://stat.ethz.ch/pipermail/r-help/2006-May/106308.html) 提出的解决方案;

  6. Marc Schwartz (https://stat.ethz.ch/pipermail/r-help/2006-May/106351.html) 对 Gabor Grothendieck 解决方案的解释 - 非常好的解释;

  7. “R Inferno”书的第 8.1.28 和 8.1.29 节 (www.burns-stat.com/pages/Tutor/R_inferno.pdf) - 我已经尝试了他使用 storage.mode() 的建议,但并没有真正解决问题,因为通过storage 强制不影响对象的class(更不用说它不包括强制属性清除操作,例如子集和索引;

  8. http://adv-r.had.co.nz/Data-structures.html#attributes;

  9. http://stat.ethz.ch/R-manual/R-devel/library/base/html/attributes.html;

  10. http://cran.r-project.org/doc/manuals/r-devel/R-lang.html#Copying-of-attributes.

附:我相信这个问题是一般性的,所以我现在还没有提供一个可重复的例子。我希望可以在没有这样的例子的情况下回答这个问题,但如果没有,请告诉我。

【问题讨论】:

  • 没有任何问题没有受益于可重现的示例。通过阅读代码来调试代码比运行代码要困难得多。如果上面的代码是可运行的,我会收到错误:could not find function "base64"。这个问题看起来并不简单或孤立,您似乎已将多个问题合并到一篇文章中。你把这个问题变成了一个非常非常难以回答的问题。我建议编辑它。
  • @MrFlick:很抱歉给您带来麻烦,感谢您的帮助!我提供的代码并不意味着可运行,因为我认为有时查看代码而不是运行它就足够了。因此,我故意将代码最小化以仅显示基本部分。至于base64(),它是来自RCurl 的函数。我会考虑编辑这个问题,但我仍然相信我的大部分(如果不是全部)问题都可以通过查看代码来回答。可重现示例 (RE) 的问题在于,有时某人试图解决的问题存在于特定上下文中,因此 RE 对此没有用处。
  • 当我们甚至不知道确切的错误是什么时,要求某人逐行阅读是很困难的。如果您可以将其缩小到仅一种功能,那可能会有所帮助。一般意义上的问题是不可能解决的(即“我如何让我的程序始终工作”)。可重现的示例隔离了一个可测试和可验证的特定问题。
  • @MrFlick:总的来说我同意你的看法。我了解 RE 的价值。但是,RE 的问题在于,正如您所提到的,它们不仅隔离了问题,而且同时删除了上下文。与其他功能和部分代码(上下文)的交互通常代表人们试图解决的问题的根源。因此,要求某人始终使用 RE 可能并不总是有助于解决这些问题。话虽如此,我会尽力隔离功能并更新我的问题。

标签: r object attributes attr user-defined-types


【解决方案1】:

我正在回答我自己的问题 - 好吧,目前只是部分回答:

1) 在更深入的调查和一些代码更新之后,似乎属性实际上并没有丢失(仍在尝试找出导致预期行为的更改 - 稍后会报告)。

2)我已经找出了转换后间歇性输出和丢失所有缓存数据的原因,如下。在代码的多次后续运行期间,每个转换(处理程序)函数projectAge()projectLicense()devTeamSize())的第二次运行返回NULL,因为转换已经完成:

if (<condition>) {
  ...
  message("Not processing - Transformation already performed!\n")
  return (invisible()) # <= returns NULL
}

然后返回的 NULL 被传递给saveRDS(),从而导致缓存数据丢失。

在保存转换后的对象之前,我通过result 的简单验证解决了这个问题

# the next line is problematic due to wrong assumption of always having full data returned
result <- do.call(handler, list(indicator, data2))
if (!is.null(result)) saveRDS(result, rdataFile) # <= fixed by validating incoming data

到此为止,感谢阅读!在澄清所有问题之前,我将更新此答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    相关资源
    最近更新 更多