我的数据帧test_11中有~4000个观测值,并将部分数据帧粘贴在下面:
数据帧片段
k_hidp列表示匹配的家庭,k_fihhmnnet1_dv列是他们报告的家庭收入,percentage_income_round报告每个参与者的收入对家庭总收入的贡献
我想过滤我的数据,以删除所有的k_hidp观测值,他们的集体收入在percentage_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