kittygirl
kittygirl

Reputation: 2443

How to remove rows and replace value by a complex condition?

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.

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

Answers (1)

Ronak Shah
Ronak Shah

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

Related Questions