Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类似于R中的Countifs和Sumifs_R_Conditional_Cumulative Sum - Fatal编程技术网

类似于R中的Countifs和Sumifs

类似于R中的Countifs和Sumifs,r,conditional,cumulative-sum,R,Conditional,Cumulative Sum,我应该先介绍一下。。。我不能用dplyr。它只是没有安装在我的R版本中 如何执行类似于R中的countifs或sumifs函数的操作 P1 | P2 | Match | Same | count_of_friends M | F | FALSE | FALSE| 6 M | M | TRUE | TRUE | 7 F | M | FALSE | FALSE| 10 F | F | TRUE | FALSE| 2 我基本上是在寻找类似EXCEL

我应该先介绍一下。。。我不能用dplyr。它只是没有安装在我的R版本中

如何执行类似于R中的
countifs
sumifs
函数的操作

   P1 | P2 | Match | Same | count_of_friends

   M  | F  | FALSE | FALSE| 6
   M  | M  | TRUE  | TRUE | 7
   F  | M  | FALSE | FALSE| 10
   F  | F  | TRUE  | FALSE| 2
我基本上是在寻找类似EXCEL的东西

SUMIFS(Match == Same; count_of_friends)
如果两个人都是同性,我想计算朋友总数;如果P1是女性,我想计算朋友总数

然后我还想了解如何只计算朋友数超过5的实例,等等


你在R怎么做

下面是一种基本R方法:

第一个问题,根据逻辑向量
P1==P2
对数据帧进行子集划分,并对第5列中的值求和

sum(df[with(df, P1 == P2), 5])
#output
9
第二个问题,根据逻辑向量
count\u of_friends>5
对数据帧进行子集划分,并检查结果数据帧的行数:

nrow(df[with(df, count_of_friends > 5),])
#output
3
数据:


我们可以使用
dplyr
对'P1'值等于'P2'的行进行
筛选,然后
通过取
总和来汇总
朋友的计数

library(dplyr)
df %>%
   filter(P1 == P2) %>% 
   summarise(Sum = sum(count_of_friends))
#   Sum
#1   9

对于第二部分,我们对“count\u friends”进行
筛选
,并获得
nrow

df %>%
   filter(count_of_friends > 5) %>%
   nrow
#[1] 3

你的预期产量是多少?不是每个人都熟悉函数
countifs
sumifs
啊,很抱歉,这也是朋友的总数。昨天你问了一个类似的问题,但没有说清楚。对不起,我不确定这是怎么回事。与其说是一个具体问题,不如说是一个一般问题。对于满足另一列条件的实例,如何将一列中的整数相加?那么如何计数呢?@Kimberly你总共有9个问题,其中你没有接受任何关于堆栈溢出的答案。所有问题的质量都很差,并且没有可复制的示例。请阅读SO指南:。如果这种情况持续下去,你就有可能进一步的问题被忽视。
df %>%
   filter(count_of_friends > 5) %>%
   nrow
#[1] 3