如何有条件地对一组中具有一定值的观测值进行计数?

流氓77

我正在使用以下数据框:

Year  Month      Day   X      Y      Color
2018  January    1     4.5    6       Red
2018  January    4     3.2    8.1     Red
2018  January    11    1.1    2.3     Blue
2018  February   7     5.4    2.2     Blue
2018  February   15    1.5    4.4     Red
2019  January    3     8.6    2.3     Red
2019  January    22    1.1    2.5     Blue
2019  January    23    5.5    7.8     Red
2019  February   5     6.9    1.1     Red
2019  February   10    1.8    1.3     Red

我正在寻找一个新列,该列指示给定月份x大于y并且颜色为“红色”的观察次数。

Year  Month      Day   X      Y       Color   XGreaterThanYCount
2018  January    1     4.5    6        Red       0
2018  January    4     3.2    8.1      Red       0
2018  January    11    1.1    2.3      Blue      0
2018  February   7     5.4    2.2      Blue      0
2018  February   15    1.5    4.4      Red       0
2019  January    3     8.6    2.3      Red       1
2019  January    22    1.1    2.5      Blue      1
2019  January    23    5.5    7.8      Red       1
2019  February   5     6.9    1.1      Red       2
2019  February   10    1.8    1.3      Red       2

我前不久发布了与此类似的问题,我要重新发布,因为我不得不稍微调整一下问题。

阿克伦

我们可以按组创建一个逻辑表达式(X > Y和(&Color == "Red")并获取sum该逻辑表达式的

library(dplyr)
df1 %>% 
   group_by(Year, Month) %>% 
   mutate(XGreaterThanYCount = sum(X > Y & Color == 'Red')) %>%
   ungroup

-输出

# A tibble: 10 x 7
#    Year Month      Day     X     Y Color XGreaterThanYCount
#   <int> <chr>    <int> <dbl> <dbl> <chr>              <int>
# 1  2018 January      1   4.5   6   Red                    0
# 2  2018 January      4   3.2   8.1 Red                    0
# 3  2018 January     11   1.1   2.3 Blue                   0
# 4  2018 February     7   5.4   2.2 Blue                   0
# 5  2018 February    15   1.5   4.4 Red                    0
# 6  2019 January      3   8.6   2.3 Red                    1
# 7  2019 January     22   1.1   2.5 Blue                   1
# 8  2019 January     23   5.5   7.8 Red                    1
# 9  2019 February     5   6.9   1.1 Red                    2
#10  2019 February    10   1.8   1.3 Red                    2

base Rave

df1$XGreaterThanYCount <-  with(df1, ave(X > Y & Color == "Red", 
             Year, Month, FUN = sum))

数据

df1 <- structure(list(Year = c(2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 
2019L, 2019L, 2019L, 2019L), Month = c("January", "January", 
"January", "February", "February", "January", "January", "January", 
"February", "February"), Day = c(1L, 4L, 11L, 7L, 15L, 3L, 22L, 
23L, 5L, 10L), X = c(4.5, 3.2, 1.1, 5.4, 1.5, 8.6, 1.1, 5.5, 
6.9, 1.8), Y = c(6, 8.1, 2.3, 2.2, 4.4, 2.3, 2.5, 7.8, 1.1, 1.3
), Color = c("Red", "Red", "Blue", "Blue", "Red", "Red", "Blue", 
"Red", "Red", "Red")), class = "data.frame", row.names = c(NA, 
-10L))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何有条件地将一列填充到列表中另一列中的值?

有条件地对大熊猫groupby对象中的值进行计数

根据R中一列中的连续值有条件地插入行

如何使用另一列中的值有条件地更改DF值

如何使用dplyr有条件地按组更改列中的值?

如何在R中使用相同的值有条件地删除观测值

有条件的累计和达到一定值时查找行

有条件地填充同一组内的密度图

根据每个组的另一个查找表有条件地对一个数据帧的值进行插值?

有条件地在数据框中填充一个值

通过查看最后一组有条件地确定列的值

R中的gsubfn,如何有条件地仅替换一组字符串中的第二组数字

如何有条件地将具有多个值的两行合并在一起并在R中进行突变?

如何在python中有条件地选择上一行的值?

仅当其他列具有条件时,才对列中的唯一值进行计数

如何从具有一定条件的多对多关系中获取所有值

有条件地验证Rails中布尔值的唯一性

流星-有条件地返回一组事件处理程序

如何有条件地选择一个聚合函数的返回值?

如何有条件地回显多维数组的一列中的一个或所有值?

通过计数有条件地替换字符值

仅当该表单组中的另一个特定表单控件具有值时,才根据需要有条件地设置特定表单控件(较大表单组的一部分)

Oracle 中 regexp_like 的匹配模式以有条件地包含一组字符

如果单元格内容是一组并且我想查看其中是否有值,如何有条件地从 Pandas 数据框中获取一行?

如何有条件地从一个值中减去

在同一列中,将每个值与具有条件的先前多个值进行比较

在 Angular 中动态创建的字段中有条件地更改字段时,如何推送相同的一组值?

如何有条件地将一组道具添加到反应组件中?

React – 如何有条件地返回一个值(或回退)