0

当我运行查询

mongo.cursor.to.data.frame(cursor)

使用 将集合中的文档提取到 R 中的数据框中rmongodb,我收到警告消息:

In mongo.cursor.to.data.frame(cursor) : This fails for most NoSQL data structures. I am working on a new solution

我检查了一些关于的文章rmongodb,我也可以找到那里提到的这条消息。此警告是否意味着生成的数据框中可能存在一些问题?

4

1 回答 1

0

源代码显示了可能出现问题的位置

mongo.cursor.to.data.frame <- function(cursor, nullToNA=TRUE, ...){

  warning("This fails for most NoSQL data structures. I am working on a new solution")

  res <- data.frame()
  while ( mongo.cursor.next(cursor) ){
    val <- mongo.bson.to.list(mongo.cursor.value(cursor))

    if( nullToNA == TRUE )
      val[sapply(val, is.null)] <- NA

    # remove mongo.oid -> data.frame can not deal with that!
    val <- val[sapply(val, class) != 'mongo.oid']

    res <- rbind.fill(res, as.data.frame(val, ... ))

  }
  return( as.data.frame(res) )
}

我们可以看到它正在使用plyr::rbind.filldata.frames rbind。所以这一切都归结为传入的内容rbind.fill,即val

并且val是 的结果val <- mongo.bson.to.list(mongo.cursor.value(cursor))

因此,只要as.data.frame(val, ...)可以处理您传递给它的列表结构就可以了。

但是,很容易设想一个NoSQL数据结构会失败:

## consider the JSON structure
## [{"a":[1,2,3],"b":["a","b","c"]},{"d":[1.1,2.2,3.3],"e":[["nested","list"]]}] 

##Which in R is the same as
lst = list(list(a = c(1L,2L,3L),
                b = c("a","b","c")),
           list(d = c(1.1, 2.2, 3.3),
                e = list(c("nested", "list"))))

## this errors when coerced to a data.frame
as.data.frame(lst)
Error in data.frame(d = c(1.1, 2.2, 3.3), e = list(c("nested", "list")),  : 
  arguments imply differing number of rows: 3, 2

在这一点上我应该提到mongolite包,它通常更快,但再次返回一个data.frame.

还有我对 mongolite 的扩展,mongolitedt(还没有在 CRAN 上)它仍然更快并且检索数据,但再次受到结果的限制,必须强制转换为data.table

于 2016-04-24T11:35:37.193 回答