Reputation: 2443
The sample data as below:
> data.frame(age = c(13, 16, 13,18,16),math = c(4, 7, 8,6,6),total = c(5, 3, 6,5,7))
age math total
1 13 4 5
2 16 7 3
3 13 8 6
4 18 6 5
5 16 6 7
The process is:for each age
, keep only one row which math
>5,replace total
by sum total
.
math
=4,not >5,then keep this row. math
=6,>5,then looking for another row with the same age
16 and
math
>5,which means row 5.Then (7+3)=10,replace total
of row 2 by
10
,and delete row 5. age
is row 1,but
math
of row 1 <5,then keep row 3 as the same. math
>5,but no match age
18,then keep this row. The expected result as below:
age math total
1 13 4 5
2 16 7 10
3 13 8 6
4 18 6 5
How to do it?
Upvotes: 0
Views: 33
Reputation: 389047
Using dplyr
, we can divide the problem into two parts. For the first part we filter
where math > 5
, group_by
age
and take first
value of math
and sum
of total
and bind the rows where math <= 5
.
library(dplyr)
df %>%
filter(math > 5) %>%
group_by(age) %>%
summarise(math = first(math),
total = sum(total)) %>%
bind_rows(df %>% filter(math <= 5))
# age math total
# <dbl> <dbl> <dbl>
#1 13 8 6
#2 16 7 10
#3 18 6 5
#4 13 4 5
Upvotes: 1