英文:
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 <- c(1,2,3,4,5,6,7,1,1,2)
# creating second column
second <- c(88,52,-28,-19,39,86,-31,10,-88,-52)
df<-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 %>%
mutate(abs = abs(second)) %>%
group_by(first, abs) %>%
filter(n() == 2 & (sum(second > 0) == 1 & sum(second < 0) == 1)) %>%
ungroup()
# A tibble: 4 × 3
first second abs
<dbl> <dbl> <dbl>
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 %>%
group_by(first, abs_second = abs(second)) %>%
filter(n() == 2, sum(second) == 0) %>%
ungroup() %>%
select(-abs_second)
which gives
# A tibble: 4 × 2
first second
<dbl> <dbl>
1 1 88
2 2 52
3 1 -88
4 2 -52
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论