Question: The question was to find the factorial and this is what I did

```
//Factorial of n numbers

#include<stdio.h>
int main()
{
    int num, i, fact, s;
    printf("Enter the value: ");
    scanf("%d",&num);
    fact = num;
    i = num;
    while(i>1)
    {
        s = i-1;
        fact = fact * s;
    }
    i--;

    printf("The factorial of the number is %d.",fact);
}

```

Hii, I was trying to find the factorial of a number n. The code is not showing any error but I am not getting the required solution. The output screen showed nothing but the input statement. What is wrong with my code? If anyone could help, that would be great.

Thank you in advance!

Answer: 
You can multiply by a logical vector to get a conditional sum:

```
library(dplyr)

df %>% 
  group_by(subs_id) %>% 
  summarize(
    ttl_amount = sum(amount), 
    amount = sum(amount * (flag == "taker"))
  )

```

```
# A tibble: 3 × 3
  subs_id ttl_amount amount
    <dbl>      <dbl>  <dbl>
1       1         25     10
2       2         30      0
3       3         30     20

```
Same as yours, but use `left_join` afterwards to add the `amount` column from the original

```r
library(tidyverse)

t <- tibble(subs_id=c(1,1,2,3,3),
       amount=c(15,10,30,20,10),
       flag=c("target", "taker", "target", "taker", "target"))

t %>%
  group_by(subs_id)%>%
  summarise(ttl_amount=sum(amount)) %>%
  left_join(select(filter(t, flag=="taker"), "subs_id", "amount", "flag"), by="subs_id") %>%
  select(-flag) %>%
  replace_na(list(amount=0))

```

yields:

```r
subs_id   ttl_amount   amount
1   25  10      
2   30  0       
3   30  20  

```