使用dplyr在r中的sumproduct

发布于 2025-02-11 08:48:53 字数 1269 浏览 2 评论 0原文

使用dplyr,我试图在r两列中求和,因为它可以在Excel中完成,但是尽管我尝试了几种攻击,但没有一个正常工作。

我的一小部分数据框(几乎61000行和20列)看起来像:

df <-structure(list(dem_sect = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("AB", 
"EP", "FE", "MF", "PA"), class = "factor"), cod_estr = c("a", 
"a", "b", "b", "c", "b", "b", "b", "a", "b", "c", "b", "b", "a", 
"b", "c", "c", "b", "c"), sect_estr = structure(c(10L, 10L, 11L, 
11L, 12L, 11L, 11L, 11L, 10L, 11L, 12L, 11L, 11L, 10L, 11L, 12L, 
12L, 11L, 12L), .Label = c("ABa", "ABb", "ABc", "EPa", "EPb", 
"EPc", "FEa", "FEb", "FEc", "MFa", "MFb", "MFc", "PAa", "PAb", 
"PAc"), class = "factor"), area = c(14L, 14L, 24L, 24L, 11L, 
24L, 24L, 24L, 14L, 24L, 11L, 24L, 24L, 14L, 24L, 11L, 11L, 24L, 
11L), kg_med = c(5.88125, 5.88125, 6.8505, 6.8505, 
3.0852, 6.8505, 6.8505, 6.8505, 5.88125, 6.8505, 3.0852, 6.8505, 
6.8505, 5.88125, 6.8505, 3.0852, 3.0852, 6.8505, 3.0852)), class = "data.frame", row.names = c(NA, 
-19L))

我想添加一个新列(例如 sumprod ),这将是 unique unique < /strong>两个“ afear” “ kg_med” “ dem_sect” 分组的的值

将是一个具有值<值<的dem_sect“ dem_sect ” 代码> 280.69 (如果我在Excel中正确进行计算),

任何帮助都将受到欢迎。提前致谢。

Using dplyr, I´m trying to sumproduct in R two columns as it can be done in Excel but although I´ve tried several aproaches, none worked properly.

A small piece of my dataframe (almost 61000 rows and 20 columns) looks like this:

df <-structure(list(dem_sect = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("AB", 
"EP", "FE", "MF", "PA"), class = "factor"), cod_estr = c("a", 
"a", "b", "b", "c", "b", "b", "b", "a", "b", "c", "b", "b", "a", 
"b", "c", "c", "b", "c"), sect_estr = structure(c(10L, 10L, 11L, 
11L, 12L, 11L, 11L, 11L, 10L, 11L, 12L, 11L, 11L, 10L, 11L, 12L, 
12L, 11L, 12L), .Label = c("ABa", "ABb", "ABc", "EPa", "EPb", 
"EPc", "FEa", "FEb", "FEc", "MFa", "MFb", "MFc", "PAa", "PAb", 
"PAc"), class = "factor"), area = c(14L, 14L, 24L, 24L, 11L, 
24L, 24L, 24L, 14L, 24L, 11L, 24L, 24L, 14L, 24L, 11L, 11L, 24L, 
11L), kg_med = c(5.88125, 5.88125, 6.8505, 6.8505, 
3.0852, 6.8505, 6.8505, 6.8505, 5.88125, 6.8505, 3.0852, 6.8505, 
6.8505, 5.88125, 6.8505, 3.0852, 3.0852, 6.8505, 3.0852)), class = "data.frame", row.names = c(NA, 
-19L))

I want to add a new column (called for example sumprod) which would be the sumproduct of the unique values of both "area" and "kg_med" grouped by "dem_sect"

The expect output will be a column with the value 280.69 (If I did the calculation correctly in Excel)

Any help will be more than welcome. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牛↙奶布丁 2025-02-18 08:48:53
library(tidyverse)

df %>% 
  group_by(dem_sect) %>% 
  filter(!duplicated(area, kg_med)) %>% 
  summarise(sumproduct = sum(area * kg_med)) %>% 
  as.data.frame() # to view the result with more significant digits

#>   dem_sect sumproduct
#> 1       MF   280.6867
library(tidyverse)

df %>% 
  group_by(dem_sect) %>% 
  filter(!duplicated(area, kg_med)) %>% 
  summarise(sumproduct = sum(area * kg_med)) %>% 
  as.data.frame() # to view the result with more significant digits

#>   dem_sect sumproduct
#> 1       MF   280.6867
梦太阳 2025-02-18 08:48:53
df %>% 
  group_by(dem_sect) %>% 
  distinct(area, kg_med) %>% 
  summarise(sumprod=sum(area*kg_med))
  dem_sect sumprod
  <fct>      <dbl>
1 MF          281.

该解决方案假设区域的每个值都与kg_med和vice-vices-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vice-vices-vice-vices-vice-vice-vices-vices-vice-vices-vices-vice-vices-vices-vices-vices-vices-vices-vices-vices-vices-vices-vices-vices-vices-vices-vessa关联。在两个列中每个列中每个列中具有不同数量的唯一值的所需行为尚未很好地指定。

检查答案的准确性超过三个重要数字...

old <- options(pillar.sigfig = 7)
df %>% 
  group_by(dem_sect) %>% 
  distinct(area, kg_med) %>% 
  summarise(sumprod=sum(area*kg_med))

# A tibble: 1 × 2
  dem_sect  sumprod
  <fct>       <dbl>
1 MF       280.6867

options(old)
df %>% 
  group_by(dem_sect) %>% 
  distinct(area, kg_med) %>% 
  summarise(sumprod=sum(area*kg_med))
  dem_sect sumprod
  <fct>      <dbl>
1 MF          281.

This solution assumes that each value of area is associated with a single value of kg_med and vice-versa. The desired behaviour with different numbers of unique values in each of the two columns is not well specified.

To check the accuracy of the answer to more than three significant digits...

old <- options(pillar.sigfig = 7)
df %>% 
  group_by(dem_sect) %>% 
  distinct(area, kg_med) %>% 
  summarise(sumprod=sum(area*kg_med))

# A tibble: 1 × 2
  dem_sect  sumprod
  <fct>       <dbl>
1 MF       280.6867

options(old)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文