在R中如何进行条件求和

huangapple go评论64阅读模式
英文:

how to do conditional summation in R

问题

I am trying to do a conditional summation based on a table that looks like this:

在R中如何进行条件求和]1

我正在尝试基于这样的表格进行条件求和:

I am trying to do a summation of the column "value" and group by Location. Normally I would just do this:

通常情况下,我只需这样做:

data <- file %>%
group_by(location, date) %>%
summarize(value = sum (value))

However, only for the Location "Central" I would like to exclude the Program "B". So I tried this way, but it did not work:

但是,仅对于“Central”位置,我想排除“B”程序。所以我尝试了这种方式,但没有成功:

data <- file %>%
group_by(location, date) %>%
summarize(value =
case_when(location == "Central" ~ filter(program != "B")),
TRUE ~ sum(value)
)

If someone could please help me with the code above, I would much appreciate that.
Thank you 在R中如何进行条件求和

如果有人能帮助我处理上面的代码,我将不胜感激。
谢谢 在R中如何进行条件求和

英文:

I am trying to do a conditional summation based on a table that looks like this:

在R中如何进行条件求和]1

I am trying to do a summation of the column "value" and group by Location. Normally I would just do this:

data &lt;- file %&gt;%
group_by(location, date) %&gt;%
summarize(value = sum (value))

However, only for the Location "Central" I would like to exclude the Program "B". So I tried this way, but it did not work:

data &lt;- file %&gt;%
group_by(location, date) %&gt;%
summarize(value = 
case_when(location == &quot;Central&quot; ~ filter(program != &quot;B&quot;)),
          TRUE ~ sum(value)
)

If someone oculd please help me with the code above, I would much appreciate that.
Thank you 在R中如何进行条件求和

EDIT:
Here is the reproducible data using dput:

structure(list(pid = c(123, 123, 123, 123, 123, 123, 123, 
123, 123, 123), program = c(&quot;A&quot;, &quot;A&quot;, 
&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, 
&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, 
&quot;A&quot;, &quot;A&quot;), location = c(&quot;Central&quot;, 
&quot;Central&quot;, &quot;Central&quot;, &quot;Central&quot;, &quot;Central&quot;, &quot;Central&quot;, &quot;Central&quot;, 
&quot;Central&quot;, &quot;Central&quot;, &quot;Central&quot;), locationid = c(&quot;123-Central&quot;, 
&quot;123-Central&quot;, &quot;123-Central&quot;, &quot;123-Central&quot;, &quot;123-Central&quot;, 
&quot;123-Central&quot;, &quot;123-Central&quot;, &quot;123-Central&quot;, &quot;123-Central&quot;, 
&quot;123-Central&quot;), date = structure(c(1302480000, 1305072000, 1307750400, 
1310342400, 1313020800, 1315699200, 1318291200, 1323561600, 1326240000, 
1328918400), tzone = &quot;UTC&quot;, class = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;)), 
    value = c(37207.43, -56936.95, -52871, 6980.05, 10703.16, 
    4006.1, 6505.3, 9661.29, 6897.26, 7212.87)), row.names = c(NA, 
-10L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))

答案1

得分: 2

filter 作用于整个数据框。你可以首先进行筛选:

file |&gt;
  filter(!(program == &quot;B&quot; &amp; location == &quot;Central&quot;)) |&gt;
  group_by(location, date) |&gt;
  summarize(value = sum(value))

或者你可以在 sum 中使用向量子集函数,像这样:

data &lt;- file |&gt;
  group_by(location, date) |&gt;
  summarize(value = 
    case_when(
      location == &quot;Central&quot; ~ sum(value[program != &quot;B&quot;]),
      TRUE ~ sum(value)
    )
  )

但你不能在向量/列上调用 filter。也不能像这样将其用作结果,location == &quot;Central&quot; ~ filter(program != &quot;B&quot;),当你希望结果是一个总和时。

英文:

filter works on a whole data frame. You can filter first:

file |&gt;
  filter(!(program == &quot;B&quot; &amp; location == &quot;Central&quot;)) |&gt;
  group_by(location, date) |&gt;
  summarize(value = sum (value))

Or you can use vector subsetting functions like [ inside the sum like this:

data &lt;- file |&gt;
  group_by(location, date) |&gt;
  summarize(value = 
    case_when(
      location == &quot;Central&quot; ~ sum(value[program != &quot;B&quot;]),
      TRUE ~ sum(value)
    )
  )

But you can't call filter on a vector/column. Nor can you use it like a result, location == &quot;Central&quot; ~ filter(program != &quot;B&quot;) when you want the result to be a sum.

huangapple
  • 本文由 发表于 2023年6月6日 03:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定