筛选具有正值和适当负值的行。

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

Filter rows with positive and appropriate negative values

问题

在R中,我想要使用"first"列的group_by筛选出在"second"列中存在正值和相应的负值的行。
所以在下面的情况下,前两行和最后两行。

  1. # 创建第一列
  2. first <- c(1,2,3,4,5,6,7,1,1,2)
  3. # 创建第二列
  4. second <- c(88,52,-28,-19,39,86,-31,10,-88,-52)
  5. df <- data.frame(first, second)

结果应该是

  1. data.frame(first=c(1,2,1,2), second=c(88,52,-88,-52))
英文:

In R I want to filter with a group_by of column "first" the rows where there is a positive value in "second" and the appropriated negative value in "second".
So in the case below the first two rows and the last two rows.

  1. # creating first column
  2. first &lt;- c(1,2,3,4,5,6,7,1,1,2)
  3. # creating second column
  4. second &lt;- c(88,52,-28,-19,39,86,-31,10,-88,-52)
  5. df&lt;-data.frame(first, second)

Result should be

  1. data.frame(first=c(1,2,1,2), second=c(88,52,-88,-52))

答案1

得分: 2

你可以在 ave 中使用 %in%intersect

  1. df[ave(df$second, df$first, FUN=\(x) x %in% intersect(x, -x)) == 1,]
  2. # first second
  3. #1 1 88
  4. #2 2 52
  5. #9 1 -88
  6. #10 2 -52
英文:

You can use %in% with intersect in ave.

  1. df[ave(df$second, df$first, FUN=\(x) x %in% intersect(x, -x)) == 1,]
  2. # first second
  3. #1 1 88
  4. #2 2 52
  5. #9 1 -88
  6. #10 2 -52

答案2

得分: 1

  1. library(dplyr)
  2. df %>%
  3. mutate(abs = abs(second)) %>%
  4. group_by(first, abs) %>%
  5. filter(n() == 2 & (sum(second > 0) == 1 & sum(second < 0) == 1)) %>%
  6. ungroup()
英文:

First create a column representing the absolute values of second, then group_by both first and the abs column. filter for groups that have exactly two entries while one of them is positive and the other one is negative.

  1. library(dplyr)
  2. df %&gt;%
  3. mutate(abs = abs(second)) %&gt;%
  4. group_by(first, abs) %&gt;%
  5. filter(n() == 2 &amp; (sum(second &gt; 0) == 1 &amp; sum(second &lt; 0) == 1)) %&gt;%
  6. ungroup()
  7. # A tibble: 4 &#215; 3
  8. first second abs
  9. &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  10. 1 1 88 88
  11. 2 2 52 52
  12. 3 1 -88 88
  13. 4 2 -52 52

答案3

得分: 1

这可能会有所帮助:

  1. df %>%
  2. first分组, abs_second = abs(second)) %>%
  3. 筛选(n() == 2, sum(second) == 0) %>%
  4. 解除分组() %>%
  5. 选择(-abs_second)

这将产生以下结果:

  1. # 一个数据框: 4 × 2
  2. first second
  3. 1 1 88
  4. 2 2 52
  5. 3 1 -88
  6. 4 2 -52
英文:

Probably this could help

  1. df %&gt;%
  2. group_by(first, abs_second = abs(second)) %&gt;%
  3. filter(n() == 2, sum(second) == 0) %&gt;%
  4. ungroup() %&gt;%
  5. select(-abs_second)

which gives

  1. # A tibble: 4 &#215; 2
  2. first second
  3. &lt;dbl&gt; &lt;dbl&gt;
  4. 1 1 88
  5. 2 2 52
  6. 3 1 -88
  7. 4 2 -52

huangapple
  • 本文由 发表于 2023年5月22日 20:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306271.html
匿名

发表评论

匿名网友

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

确定