Home > Back-end >  How to add new string in column row
How to add new string in column row

Time:05-30

Need to add a new String with new calculations

Strength Value
2 ml 10
5 ml 05
2 ml 30
5 ml 40
2 ml 10
5 ml 25
2 ml 30
5 ml 20
2 ml 15
5 ml 10

Now I need to add New String Total (2 ml 5 ml) in the Strength Column

so the table will look like below

Strength Value
2 ml 10
5 ml 05
Total 15
2 ml 30
5 ml 40
Total 70
2 ml 10
5 ml 25
Total 35
2 ml 30
5 ml 20
Total 50
2 ml 15
5 ml 10
Total 25

CodePudding user response:

You may try using dplyr, letting your data df

library(dplyr)

df %>%
  mutate(n = floor((1:n()-1)/2)) %>%
  group_by(n) %>%
  group_modify(., function(x, y) bind_rows(x, summarise(x, Strength = "Total",
                                                      Value = sum(Value)))) %>% 
  ungroup %>%
  select(-n)

   Strength Value
   <chr>    <int>
 1 2 ml        10
 2 5 ml         5
 3 Total       15
 4 2 ml        30
 5 5 ml        40
 6 Total       70
 7 2 ml        10
 8 5 ml        25
 9 Total       35
10 2 ml        30
11 5 ml        20
12 Total       50
13 2 ml        15
14 5 ml        10
15 Total       25

CodePudding user response:

The standard split/apply/combine method via base R,

i1 <- split(df1, f = cumsum(seq(nrow(df1)) %% 2))
do.call(rbind, 
        lapply(i1, function(i){i[nrow(i)   1,] <- data.frame(Strength = 'Total', 
                                                             Value = sum(i$Value));i}))

     Strength Value
1.1       2ml    10
1.2       5ml     5
1.3     Total    15
2.3       2ml    30
2.4       5ml    40
2.1     Total    70
3.5       2ml    10
3.6       5ml    25
3.1     Total    35
4.7       2ml    30
4.8       5ml    20
4.1     Total    50
5.9       2ml    15
5.10      5ml    10
5.1     Total    25

CodePudding user response:

Using by

do.call(
  rbind,
  by(
    df,
    rep(1:(nrow(df)/2),each=2),
    function(x){
      rbind(
        x,
        data.frame(
          "Strength"="Total",
          "Value"=sum(x$Value)
        )
      )
    }
  )
)

     Strength Value
1.1      2 ml    10
1.2      5 ml     5
1.3     Total    15
2.3      2 ml    30
2.4      5 ml    40
2.1     Total    70
3.5      2 ml    10
3.6      5 ml    25
3.1     Total    35
4.7      2 ml    30
4.8      5 ml    20
4.1     Total    50
5.9      2 ml    15
5.10     5 ml    10
5.1     Total    25

CodePudding user response:

df %>%
  # create a group identifier:
  mutate(grp = rep(1:(nrow(.)/2), each=2)) %>%
  # for each group:
  group_by(grp) %>%
  # calculate the sum of `Value`:
  summarise(Value = sum(Value)) %>%
  # create a column `Strength` with value `Total`:
  mutate(Strength = "Total") %>%
  # bind result back to df while creating, again, group identifier:
  bind_rows(.,df %>% mutate(grp = rep(1:(nrow(.)/2),each=2))) %>%
  # order by group:
  arrange(grp) %>%
  # remove grouping varible:
  select(-grp)
# A tibble: 6 × 2
  Value Strength
  <dbl> <chr>   
1    15 Total   
2    10 2 ml    
3     5 5 ml    
4    70 Total   
5    30 2 ml    
6    40 5 ml 
  •  Tags:  
  • r
  • Related