本文介绍了细分列表-多变的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常将数据按一个或多个变量分组,每组中都有多个注册.我希望从数据框中根据各种标准选择组.

I often have data that is grouped by one or more variables, with several registrations within each group. From the data frame, I wish to select groups according to various criteria.

我通常使用split-sapply-rbind方法,其中我使用逻辑向量从列表中提取元素.

I commonly use a split-sapply-rbind approach, where I extract elements from a list using a logical vector.

这是一个小例子.我从一个带有一个分组变量("group")的数据帧开始,我希望选择最大质量小于45的组:

Here is a small example. I start with a data frame with one grouping variable ('group'), and I wish to select groups that have a maximum mass of less than 45:

dd <- data.frame(group = rep(letters[1:3], each = 5),
                 mass = c(rnorm(5, 30), rnorm(5, 50),
                          rnorm(5, 40)))
    dd2 <- split(x = dd, f = dd$group)
    dd3 <- dd2[sapply(dd2, function(x) max(x$mass) < 45)]
    dd4 <- do.call(rbind, dd3)

我刚刚开始使用plyr,现在我想知道:
是否有一个只有plyr的替代方案可以实现这一目标?

I have just started to use plyr, and now I wonder:
is there a plyr-only alternative to achieve this?

推荐答案

至少在这种情况下,结果相同

At least in this situation this gives the same result

library(plyr)
dd5 <- ddply(dd,.(group),function(x) x[max(x$mass)<45,])

all(dd4==dd5)
[1] TRUE

这篇关于细分列表-多变的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:21