ddply:推还是拉?

发布于 2024-10-02 20:00:42 字数 50 浏览 0 评论 0原文

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

Does ddply push or pull when grouping data?
I.e, does it involve many passes over the data frame, or just one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅紫色的梦幻 2024-10-09 20:00:42

如果您查看代码,您会看到该函数的一般结构:

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 实际上比列表更复杂一些 - 它是一个指向原始数据的指针和一个索引列表。每当您请求列表中的一部分时,它都会查找匹配的索引并提取适当的数据。这避免了数据的多个副本四处浮动。您可以使用 getAnywhere("splitter_d")plyr:::splitter_d 查看其功能。

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

所有 plyr 函数都使用相同的
拆分-应用-组合策略:他们
将输入分成更简单的部分,
将 .fun 应用于每个部分,然后
将这些片段组合成一个数据
结构。该函数拆分列表
按元素并组合结果
到数据框中。如果没有
结果,那么这个函数将
返回具有零行的数据框并且
列(data.frame())。

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

If you take a look at the code, you see the general structure of the function:

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>

so it basically rearranges the variables in a format that's easier to use, then breaks the data into pieces, and then use ldply on those pieces. Those pieces are generated by the function splitter_d. Pieces is actually a little bit more sophisticated than a list - it's a pointer to the original data and a list of indices. Whenever you request a piece of the list, it looks up the matching indices and extracts the appropriate data. This avoids having multiple copies of the data floating around. You can see how that functions using getAnywhere("splitter_d") or plyr:::splitter_d.

ldply passes once over every piece of data. After that, it combines everything back into a dataframe. Actually, in the help files of ldply is written:

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()).

I couldn't say it better myself. And miracle, the first sentence is to be found on the help page for ddply as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文