-1

I should preface this... I cannot use dplyr. It is just not installing in my version of R.

How would I do an action similar to a countifs or sumifs function in R?

   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

I would essentially be looking for something similar to EXCEL's

SUMIFS(Match == Same; count_of_friends)

I want to find the sum of friends if both individuals are of the same gender, or the sum of the counts of friends if the P1 is a Female.

I then also want to find out how to count only the instances where the count of friends is over 5, etc.

How would you do this in R?

Kimberly
  • 69
  • 8
  • What's your expected output? Not everyone is familiar with functions `countifs` and `sumifs` – pogibas Mar 04 '18 at 08:51
  • Ah sorry it wound be the sum of friends – Kimberly Mar 04 '18 at 08:52
  • Yesterday too you asked a similar question without much clarity – akrun Mar 04 '18 at 08:57
  • I'm sorry - I'm not sure how it is unclear. It is not exactly one specific question so much as a general. How would I sum up the integers in a column for only the instances where a condition for another column is met? And how would this be done on count? – Kimberly Mar 04 '18 at 09:01
  • @Kimberly you have a total of 9 questions in which you have not accepted any answers on stack overflow. All of the questions are of poor quality and without a reproducible example. Please read SO guidelines:[1](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), [2](https://stackoverflow.com/help/how-to-ask), [3](https://stackoverflow.com/help/someone-answers). If this persists you are risking your further questions being ignored. – missuse Mar 04 '18 at 09:14

2 Answers2

0

Here is an approach in base R:

first question, subset the data frame according to a logical vector P1 == P2 and sum the values in column 5

sum(df[with(df, P1 == P2), 5])
#output
9

second question, subset the data frame according to a logical vector count_of_friends > 5 and check the number of rows of the resulting data frame:

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

data:

> dput(df)
structure(list(P1 = structure(c(2L, 2L, 1L, 1L), .Label = c("F", 
"M"), class = "factor"), P2 = structure(c(1L, 2L, 2L, 1L), .Label = c("F", 
"M"), class = "factor"), Match = c(FALSE, TRUE, FALSE, TRUE), 
    Same = c(FALSE, TRUE, FALSE, FALSE), count_of_friends = c(6, 
    7, 10, 2)), .Names = c("P1", "P2", "Match", "Same", "count_of_friends"
), row.names = c(NA, -4L), class = "data.frame")
missuse
  • 19,056
  • 3
  • 25
  • 47
0

We can use dplyr to filter rows that have 'P1' value equal to 'P2' and then summarise the 'count_of_friends' by taking the sum

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

For the second part, we do the filter on the 'count_of_friends' and get the nrow

df %>%
   filter(count_of_friends > 5) %>%
   nrow
#[1] 3
akrun
  • 874,273
  • 37
  • 540
  • 662