我有下面的数据集,希望通过子集找到最大值
数据集
StudentID Indicator Value
100 N 30
100 N 35
100 N 28
100 Y 20
100 N 29
100 N 60
200 N 40
200 N 35
200 Y 20
200 N 24
200 N 35
我希望结果如下:
结果
StudentID Indicator Value Max
100 N 30 35
100 N 35 35
100 N 28 35
100 N 29 60
100 N 60 60
200 N 40 40
200 N 35 40
200 N 24 35
200 N 35 35
所以本质上,每当指示符从N变为Y时,我需要将studentID和IndicatorID的行视为一个块,计算该块的最大值,然后继续下一次迭代。你知道吗
下面是一个使用
pandas
来自python
的选项。我们通过获取逻辑输出的累积和来创建一个分组变量(dat.Indicator == "Y"
),然后通过删除“Indicator”为“Y”的行来子集行,按“StudentID”、“Group”分组,用transform
获取“Value”的max
,将其分配给“Value”,并drop
不需要的列-输出
一个
base R
选项应该是ave
数据
缺少用于指示组的变量。您可以在SAS中使用
by
语句上的notsorted
选项轻松地实现这一点。你知道吗现在有很多方法可以根据组来生成平均值。
PROC SQL
通过自动将聚合值重新合并回细节行,使之变得简单。你知道吗结果:
我不知道为什么您的示例输出删除了INDICATOR='Y'的组,但是您可以添加一个
where
子句来删除它们。你知道吗在R中使用dplyr的解
数据
相关问题 更多 >
编程相关推荐