如何在列行中添加新字符串
How to add new string in column row
需要使用新计算添加新字符串
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
现在我需要在 Strength 列中添加 New String Total (2 ml + 5 ml)
所以 table 将如下所示
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
使用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
您可以尝试使用 dplyr
,让您的数据 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
通过基础 R 的标准 split/apply/combine 方法,
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
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
需要使用新计算添加新字符串
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 |
现在我需要在 Strength 列中添加 New String Total (2 ml + 5 ml)
所以 table 将如下所示
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 |
使用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
您可以尝试使用 dplyr
,让您的数据 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
通过基础 R 的标准 split/apply/combine 方法,
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
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