Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Summation over multiple rows based on a condition in R

I have a dataset like the following

name city number
A M 2
A N 3
A O 5
A P 7
B M 7
B N 8
B 0 9
B P 2

For each of of the name category, I want to sum the number of M and N value and put it in a new variable. The same goes for O and P value.
The dataset should look like the following:

name city number
A X 5
A Y 12
B X 15
B Y 11

I’m new in R programming. I have tried to use group by and mutate method but was not successful.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We could modify the values in the column ‘city’ to ‘X’, ‘Y’, and do a group by sum

library(dplyr)
df1 %>%
   group_by(name, city = case_when(city %in% c("M", "N") ~ 'X',
    city %in% c("O", "P") ~ "Y")) %>%
   summarise(number = sum(number), .groups = 'drop')

-output

# A tibble: 4 × 3
  name  city  number
  <chr> <chr>  <int>
1 A     X          5
2 A     Y         12
3 B     X         15
4 B     Y         11

data

df1 <- structure(list(name = c("A", "A", "A", "A", "B", "B", "B", "B"
), city = c("M", "N", "O", "P", "M", "N", "O", "P"), number = c(2L, 
3L, 5L, 7L, 7L, 8L, 9L, 2L)), row.names = c(NA, -8L), class = "data.frame")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading