[英]Removing matching observations where their adjacent column does not equal to 100
I have ~4000 observations in my data frame, test_11, and have pasted part of the data frame below:我的数据框 test_11 中有大约 4000 个观察值,并在下面粘贴了部分数据框:
The k_hidp column represents matching households, the k_fihhmnnet1_dv column is their reported household income and the percentage_income_rounded reports each participant's income contribution to the total household income k_hidp 列代表匹配的家庭,k_fihhmnnet1_dv 列是他们报告的家庭收入,percentage_income_rounded 报告每个参与者的收入对家庭总收入的贡献
I want to filter my data to remove all k_hidp observations where their collective income in the percentage_income_rounded does not equal 100.我想过滤我的数据以删除所有 k_hidp 观察值,其中它们在 percent_income_rounded 中的集体收入不等于 100。
So for example, the first household 68632420 reported a contribution of 83% (65+13) instead of the 100% as the other households report.例如,第一个家庭 68632420 报告了 83% (65+13) 的贡献,而不是其他家庭报告的 100%。
Is there any way to remove these household observations so I am only left with households with a collective income of 100%?有什么办法可以消除这些家庭观察结果,所以我只剩下集体收入为 100% 的家庭?
Thank you!谢谢!
Try this:尝试这个:
## 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.