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

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

Filter rows with positive and appropriate negative values

问题

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

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

结果应该是

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.

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

Result should be

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

答案1

得分: 2

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

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

You can use %in% with intersect in ave.

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

答案2

得分: 1

library(dplyr)

df %>%
  mutate(abs = abs(second)) %>%
  group_by(first, abs) %>%
  filter(n() == 2 & (sum(second > 0) == 1 & sum(second < 0) == 1)) %>%
  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.

library(dplyr)

df %&gt;% 
  mutate(abs = abs(second)) %&gt;% 
  group_by(first, abs) %&gt;% 
  filter(n() == 2 &amp; (sum(second &gt; 0) == 1 &amp; sum(second &lt; 0) == 1)) %&gt;% 
  ungroup()

# A tibble: 4 &#215; 3
  first second   abs
  &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;
1     1     88    88
2     2     52    52
3     1    -88    88
4     2    -52    52

答案3

得分: 1

这可能会有所帮助:

df %>%
    按first分组, abs_second = abs(second)) %>%
    筛选(n() == 2, sum(second) == 0) %>%
    解除分组() %>%
    选择(-abs_second)

这将产生以下结果:

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

Probably this could help

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

which gives

# A tibble: 4 &#215; 2
  first second
  &lt;dbl&gt;  &lt;dbl&gt;
1     1     88
2     2     52
3     1    -88
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:

确定