繁体   English   中英

删除相邻列不等于 100 的匹配观测值

[英]Removing matching observations where their adjacent column does not equal to 100

我的数据框 test_11 中有大约 4000 个观察值,并在下面粘贴了部分数据框:

数据框片段

k_hidp 列代表匹配的家庭,k_fihhmnnet1_dv 列是他们报告的家庭收入,percentage_income_rounded 报告每个参与者的收入对家庭总收入的贡献

我想过滤我的数据以删除所有 k_hidp 观察值,其中它们在 percent_income_rounded 中的集体收入不等于 100。

例如,第一个家庭 68632420 报告了 83% (65+13) 的贡献,而不是其他家庭报告的 100%。

有什么办法可以消除这些家庭观察结果,所以我只剩下集体收入为 100% 的家庭?

谢谢!

尝试这个:

## Creating the dataframe
df=data.frame(k_hidp = c(68632420,68632420,68632420,68632420,68632420,68632420,68632422,68632422,68632422,68632422,68632428,68632428),
              percentage_income_rounded = c(65,18,86,14,49,51,25,25,25,25,50,50))

## Loading the libraries
library(dplyr)

## Aggregating and determining which household collective income is 100%
df1 = df %>%
  group_by(k_hidp) %>%
  mutate(TotalPercentage = sum(percentage_income_rounded)) %>%
  filter(TotalPercentage == 100)

输出

> df1
# A tibble: 6 x 3
# Groups:   k_hidp [2]
    k_hidp percentage_income_rounded TotalPercentage
     <dbl>                     <dbl>           <dbl>
1 68632422                        25             100
2 68632422                        25             100
3 68632422                        25             100
4 68632422                        25             100
5 68632428                        50             100
6 68632428                        50             100

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM