r - ddply : push or pull?

标签 r plyr

ddply在分组数据时是推还是拉? 即,它是否涉及对数据帧的多次传递,还是仅一次?

最佳答案

如果你看一下代码,你会看到函数的一般结构:

function (.data, .variables, .fun = NULL, ..., .progress = "none", 
    .drop = TRUE, .parallel = FALSE) 
{
    .variables <- as.quoted(.variables)
    pieces <- splitter_d(.data, .variables, drop = .drop)
    ldply(.data = pieces, .fun = .fun, ..., .progress = .progress, 
        .parallel = .parallel)
}
<environment: namespace:plyr>

所以它基本上以更易于使用的格式重新排列变量,然后将数据分成几部分,然后对这些部分使用 ldply。这些片段是由函数 splitter_d 生成的。 Pieces 实际上比列表更复杂一些 - 它是一个指向原始数据的指针和一个索引列表。每当您请求列表中的一部分时,它都会查找匹配的索引并提取适当的数据。这避免了数据的多个副本四处 float 。您可以使用 getAnywhere("splitter_d")plyr:::splitter_d 查看其功能。

ldply 对每条数据传递一次。之后,它将所有内容组合回数据帧中。其实ldply的帮助文件里是这么写的:

All plyr functions use the same split-apply-combine strategy: they split the input into simpler pieces, apply .fun to each piece, and then combine the pieces into a single data structure. This function splits lists by elements and combines the result into a data frame. If there are no results, then this function will return a data frame with zero rows and columns (data.frame()).

我自己也说不出更好的说法。奇迹是,第一句话也可以在 ddply 的帮助页面上找到。

关于r - ddply : push or pull?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4191296/

相关文章:

r - 对按因子拆分的数据帧中的许多列应用 t 检验

javascript - 当同一页面上有 2 个 vimeo 视频时,plyr/video 不自动播放

r - 从因子和计数创建列

r - 更改 plot.dendrogram 中的叶子颜色,就像包 ape 的 plot.phylo

r - 在R google_analytics中使用过滤器

r - 如何在 R 代码中集成 Google 距离矩阵 API key ?

r - 使用 llply 将函数应用到列表 - 如何处理不合格的条目?

r - 在knitr中更改注释字体

r - 使用 R 中的 data.table 根据条件合并两行的值

r - 将对象传递给 plyr 的 rename()