Home > Back-end >  conditional combining rows and add to existing row
conditional combining rows and add to existing row

Time:03-08

here is my data frame:

df<- data.frame(age=c(10,11,12,11,11,10,11,13,13,13,14,14,15,15,15), 
                time1=c(10:24),time2=c(20:34))

I want to sum rows for age 14 and 15 and keep as age 14. my expected output would be like this:

    age time1 time2
1   10    10    20
2   11    11    21
3   12    12    22
4   11    13    23
5   11    14    24
6   10    15    25
7   11    16    26
8   13    17    27
9   13    18    28
10  13    19    29
11  14   110   160

thank you in advance.

CodePudding user response:

Here is one method - replace the 'age' where value is '15' to 14, and summarise across the columns 'time' to get the sum if the 'age' values are all '14'

library(dplyr)
df %>% 
  group_by(age = replace(age, age %in% 15, 14))  %>% 
  summarise(across(everything(),  ~if(all(age == 14))sum(.x) else .x), 
       .groups = 'drop')

-output

# A tibble: 11 × 3
     age time1 time2
   <dbl> <int> <int>
 1    10    10    20
 2    10    15    25
 3    11    11    21
 4    11    13    23
 5    11    14    24
 6    11    16    26
 7    12    12    22
 8    13    17    27
 9    13    18    28
10    13    19    29
11    14   110   160

Or using base R with colSums and subset/rbind

rbind(subset(df, !age %in% c(14, 15)),
     c(age = 14, colSums(df[df$age %in% c(14, 15), - 1])))

-output

   age time1 time2
1   10    10    20
2   11    11    21
3   12    12    22
4   11    13    23
5   11    14    24
6   10    15    25
7   11    16    26
8   13    17    27
9   13    18    28
10  13    19    29
11  14   110   160
  •  Tags:  
  • r
  • Related