当其他两列匹配时,我想取一个列变量的和,然后为这个sum结果添加一个名称不同的新行。我可以得到第一部分,但不确定如何为获得的结果添加新行.
这是我的数据
df <- structure(list(measure_name = c("Prevalence", "Prevalence", "Incidence",
"Incidence", "Deaths", "Deaths", "YLLs (Years of Life Lost)",
"YLLs (Years of Life Lost)", "YLDs (Years Lived with Disability)",
"YLDs (Years Lived with Disability)", "DALYs (Disability-Adjusted Life Years)",
"DALYs (Disability-Adjusted Life Years)", "Prevalence", "Prevalence",
"Incidence", "Incidence", "YLDs (Years Lived with Disability)",
"YLDs (Years Lived with Disability)", "DALYs (Disability-Adjusted Life Years)",
"DALYs (Disability-Adjusted Life Years)"), age_name = c("1-4 years",
"5-9 years", "1-4 years", "5-9 years", "1-4 years", "5-9 years",
"1-4 years", "5-9 years", "1-4 years", "5-9 years", "1-4 years",
"5-9 years", "1-4 years", "5-9 years", "1-4 years", "5-9 years",
"1-4 years", "5-9 years", "1-4 years", "5-9 years"), cause_name = c("Asthma",
"Asthma", "Asthma", "Asthma", "Asthma", "Asthma", "Asthma", "Asthma",
"Asthma", "Asthma", "Asthma", "Asthma", "Attention-deficit/hyperactivity disorder",
"Attention-deficit/hyperactivity disorder", "Attention-deficit/hyperactivity disorder",
"Attention-deficit/hyperactivity disorder", "Attention-deficit/hyperactivity disorder",
"Attention-deficit/hyperactivity disorder", "Attention-deficit/hyperactivity disorder",
"Attention-deficit/hyperactivity disorder"), val = c(21809765.44,
33602368.48, 10004723.65, 6417738.685, 6101.934992, 1699.9247,
524901.7761, 138969.73, 879880.8571, 1355302.883, 1404782.633,
1494272.613, 1367581.312, 14033704.42, 1314270.786, 2654899.128,
16774.31306, 171847.3209, 16774.31306, 171847.3209)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
> df
# A tibble: 20 × 4
measure_name age_name cause_name val
<chr> <chr> <chr> <dbl>
1 Prevalence 1-4 years Asthma 21809765.
2 Prevalence 5-9 years Asthma 33602368.
3 Incidence 1-4 years Asthma 10004724.
4 Incidence 5-9 years Asthma 6417739.
5 Deaths 1-4 years Asthma 6102.
6 Deaths 5-9 years Asthma 1700.
7 YLLs (Years of Life Lost) 1-4 years Asthma 524902.
8 YLLs (Years of Life Lost) 5-9 years Asthma 138970.
9 YLDs (Years Lived with Disability) 1-4 years Asthma 879881.
10 YLDs (Years Lived with Disability) 5-9 years Asthma 1355303.
11 DALYs (Disability-Adjusted Life Years) 1-4 years Asthma 1404783.
12 DALYs (Disability-Adjusted Life Years) 5-9 years Asthma 1494273.
13 Prevalence 1-4 years Attention-deficit/hyperactivity disorder 1367581.
14 Prevalence 5-9 years Attention-deficit/hyperactivity disorder 14033704.
15 Incidence 1-4 years Attention-deficit/hyperactivity disorder 1314271.
16 Incidence 5-9 years Attention-deficit/hyperactivity disorder 2654899.
17 YLDs (Years Lived with Disability) 1-4 years Attention-deficit/hyperactivity disorder 16774.
18 YLDs (Years Lived with Disability) 5-9 years Attention-deficit/hyperactivity disorder 171847.
19 DALYs (Disability-Adjusted Life Years) 1-4 years Attention-deficit/hyperactivity disorder 16774.
20 DALYs (Disability-Adjusted Life Years) 5-9 years Attention-deficit/hyperactivity disorder 171847.
以哮喘流行率为例的期望输出:
measure_name age_name cause_name val
<chr> <chr> <chr> <dbl>
1 Prevalence 1-4 years Asthma 21809765.
2 Prevalence 5-9 years Asthma 33602368.
3 Prevalence 1–9 years Asthma 55412133.
我试过的是:
Total <- df %>%
group_by(measure_name, cause_name) %>%
summarise(val = sum(val, na.rm = TRUE)) %>% arrange(cause_name, factor(measure_name, levels = c("Prevalence",
"Incidence",
"Deaths",
"YLLs (Years of Life Lost)",
"YLDs (Years Lived with Disability)",
"DALYs (Disability-Adjusted Life Years)")))
# A tibble: 10 × 3
# Groups: measure_name [6]
measure_name cause_name val
<chr> <chr> <dbl>
1 Prevalence Asthma 55412134.
2 Incidence Asthma 16422462.
3 Deaths Asthma 7802.
4 YLLs (Years of Life Lost) Asthma 663872.
5 YLDs (Years Lived with Disability) Asthma 2235184.
6 DALYs (Disability-Adjusted Life Years) Asthma 2899055.
7 Prevalence Attention-deficit/hyperactivity disorder 15401286.
8 Incidence Attention-deficit/hyperactivity disorder 3969170.
9 YLDs (Years Lived with Disability) Attention-deficit/hyperactivity disorder 188622.
10 DALYs (Disability-Adjusted Life Years) Attention-deficit/hyperactivity disorder 188622.
我得到了总数,但想要创建新的行与新的age_name,即1-9年(请参阅上面期望的输出,使用哮喘流行为例)。有人能帮忙吗?谢谢。
发布于 2022-09-19 12:52:22
如果我正确地理解了您的问题,您只需对val
进行求和,更改age_name
的值,然后使用bind_rows
将其与原始数据集联系起来。在那之后,这只是一个组织行以你想要的方式出现的问题。以下是这样一个解决方案:
df %>%
group_by(measure_name, cause_name) %>%
summarise(val = sum(val, na.rm = TRUE), age_name = "1-9 years", .groups = "drop") %>%
bind_rows(df) %>%
arrange(cause_name, factor(measure_name, levels = c("Prevalence",
"Incidence",
"Deaths",
"YLLs (Years of Life Lost)",
"YLDs (Years Lived with Disability)",
"DALYs (Disability-Adjusted Life Years)")),
age_name = factor(age_name, levels = c("1-4 years", "5-9 years", "1-9 years")))
https://stackoverflow.com/questions/73770691
复制相似问题