I wrote code which calculates:
- Decile threshold for every decile group
- Total income in the decile group
- Number of persons
- Share of decile in total income (%)
- Tax
- Share of tax (%)
But unfortunately I didn't wrote with function like e.g apply, lapply, aggregate or similar function, so my code had around 150 lines. Can anybody help me to make this code simpler with some function like apply or something similar?
Output of my code you can see in this picture:
[![`
library(dplyr)
set.seed(1444)
data1<-data.frame(sample(1000))
data2<-mutate(data1,TAX=sample.1000.*0.15)
colnames(data2)<-c("NET_INCOME","TAX")
# CALCULATION....
decili_total_income_neto<-data.frame(quantile(data2$NET_INCOME, c(.10, .20, .30, .40, .50, .60, .70, .80, .90, 1)))
ZBIR_TOTAL_NET_INCOME<-sum(data2$NET_INCOME)
ZBIR_TOTAL_TAX<-sum(data2$TAX)
#DECILE 1
t_prag_top_total_income_10<-decili_total_income_neto\[1,1\]
t_prag_top_total_income_filter_10<-filter(data2, NET_INCOME>= 0, NET_INCOME<= t_prag_top_total_income_10)
t_prag_top_total_income_filter_10_tax<-sum(t_prag_top_total_income_filter_10$TAX)
t_tax_share_10<-((t_prag_top_total_income_filter_10_tax)/ZBIR_TOTAL_TAX)*100
t_prag_top_total_income_filter_10<-sum(t_prag_top_total_income_filter_10$NET_INCOME)
t_prag_top_total_income_filter_10a<-nrow(filter(data2, NET_INCOME>= 0, NET_INCOME<= t_prag_top_total_income_10))
t_prag_top_total_income_10b<-((t_prag_top_total_income_filter_10)/ZBIR_TOTAL_NET_INCOME)*100
FINAL_DECILE_TABLE<-data.frame(cbind(t_prag_top_total_income_10,t_prag_top_total_income_filter_10,t_prag_top_total_income_filter_10a,t_prag_top_total_income_10b,t_prag_top_total_income_filter_10_tax , t_tax_share_10))
colnames(FINAL_DECILE_TABLE)<-c("Decile threshold","Total income in the decile","Number of persons in the centile","Share of the decile in total income (%)","Tax","Share tax(%)")
#DECILE 2
t_prag_top_total_income_20<-decili_total_income_neto\[2,1\]
t_prag_top_total_income_filter_20<-filter(data2, NET_INCOME> t_prag_top_total_income_10, NET_INCOME<=t_prag_top_total_income_20)
t_prag_top_total_income_filter_20_tax<-sum(t_prag_top_total_income_filter_20$TAX)
t_tax_share_20<-((t_prag_top_total_income_filter_20_tax)/ZBIR_TOTAL_TAX)*100
t_prag_top_total_income_filter_20<-sum(t_prag_top_total_income_filter_20$NET_INCOME)
t_prag_top_total_income_filter_20a<-nrow(filter(data2, NET_INCOME> t_prag_top_total_income_10, NET_INCOME<=t_prag_top_total_income_20))
t_prag_top_total_income_20b<-((t_prag_top_total_income_filter_20)/ZBIR_TOTAL_NET_INCOME)*100
FINAL_CENTILE_TABLE20<-data.frame(cbind(t_prag_top_total_income_20,t_prag_top_total_income_filter_20,t_prag_top_total_income_filter_20a,t_prag_top_total_income_20b,t_prag_top_total_income_filter_20_tax , t_tax_share_20))
colnames(FINAL_CENTILE_TABLE20)<-c("Decile threshold","Total income in the decile","Number of persons in the centile","Share of the decile in total income (%)","Tax","Share tax(%)")
FINAL_DECILE_TABLE <- rbind(FINAL_DECILE_TABLE, FINAL_CENTILE_TABLE20)
#DECILE 3
t_prag_top_total_income_30<-decili_total_income_neto\[3,1\]
t_prag_top_total_income_filter_30<-filter(data2, NET_INCOME> t_prag_top_total_income_20, NET_INCOME<=t_prag_top_total_income_30)
t_prag_top_total_income_filter_30_tax<-sum(t_prag_top_total_income_filter_30$TAX)
t_tax_share_30<-((t_prag_top_total_income_filter_30_tax)/ZBIR_TOTAL_TAX)*100
t_prag_top_total_income_filter_30<-sum(t_prag_top_total_income_filter_30$NET_INCOME)
t_prag_top_total_income_filter_30a<-nrow(filter(data2, NET_INCOME> t_prag_top_total_income_20, NET_INCOME<=t_prag_top_total_income_30))
t_prag_top_total_income_30b<-((t_prag_top_total_income_filter_30)/ZBIR_TOTAL_NET_INCOME)*100
FINAL_CENTILE_TABLE30<-data.frame(cbind(t_prag_top_total_income_30,t_prag_top_total_income_filter_30,t_prag_top_total_income_filter_30a,t_prag_top_total_income_30b,t_prag_top_total_income_filter_30_tax , t_tax_share_30))
colnames(FINAL_CENTILE_TABLE30)<-c("Decile threshold","Total income in the decile","Number of persons in the centile","Share of the decile in total income (%)","Tax","Share tax(%)")
FINAL_DECILE_TABLE <- rbind(FINAL_DECILE_TABLE, FINAL_CENTILE_TABLE30)
#DECILE 4
t_prag_top_total_income_40<-decili_total_income_neto\[4,1\]
t_prag_top_total_income_filter_40<-filter(data2, NET_INCOME> t_prag_top_total_income_30, NET_INCOME<=t_prag_top_total_income_40)
t_prag_top_total_income_filter_40_tax<-sum(t_prag_top_total_income_filter_40$TAX)
t_tax_share_40<-((t_prag_top_total_income_filter_40_tax)/ZBIR_TOTAL_TAX)*100
t_prag_top_total_income_filter_40<-sum(t_prag_top_total_income_filter_40$NET_INCOME)
t_prag_top_total_income_filter_40a<-nrow(filter(data2, NET_INCOME> t_prag_top_total_income_30, NET_INCOME<=t_prag_top_total_income_40))
t_prag_top_total_income_40b<-((t_prag_top_total_income_filter_40)/ZBIR_TOTAL_NET_INCOME)*100
FINAL_CENTILE_TABLE40<-data.frame(cbind(t_prag_top_total_income_40,t_prag_top_total_income_filter_40,t_prag_top_total_income_filter_40a,t_prag_top_total_income_40b,t_prag_top_total_income_filter_40_tax , t_tax_share_40))
colnames(FINAL_CENTILE_TABLE40)<-c("Decile threshold","Total income in the decile","Number of persons in the centile","Share of the decile in total income (%)","Tax","Share tax(%)")
FINAL_DECILE_TABLE <- rbind(FINAL_DECILE_TABLE, FINAL_CENTILE_TABLE40)
#DECILE 5
t_prag_top_total_income_50<-decili_total_income_neto\[5,1\]
t_prag_top_total_income_filter_50<-filter(data2, NET_INCOME> t_prag_top_total_income_40, NET_INCOME<=t_prag_top_total_income_50)
t_prag_top_total_income_filter_50_tax<-sum(t_prag_top_total_income_filter_50$TAX)
t_tax_share_50<-((t_prag_top_total_income_filter_50_tax)/ZBIR_TOTAL_TAX)*100
t_prag_top_total_income_filter_50<-sum(t_prag_top_total_income_filter_50$NET_INCOME)
t_prag_top_total_income_filter_50a<-nrow(filter(data2, NET_INCOME> t_prag_top_total_income_40, NET_INCOME<=t_prag_top_total_income_50))
t_prag_top_total_income_50b<-((t_prag_top_total_income_filter_50)/ZBIR_TOTAL_NET_INCOME)*100
FINAL_CENTILE_TABLE50<-data.frame(cbind(t_prag_top_total_income_50,t_prag_top_total_income_filter_50,t_prag_top_total_income_filter_50a,t_prag_top_total_income_50b,t_prag_top_total_income_filter_50_tax , t_tax_share_50))
colnames(FINAL_CENTILE_TABLE50)<-c("Decile threshold","Total income in the decile","Number of persons in the centile","Share of the decile in total income (%)","Tax","Share tax(%)")
FINAL_DECILE_TABLE <- rbind(FINAL_DECILE_TABLE, FINAL_CENTILE_TABLE50)
#DECILE 6
t_prag_top_total_income_60<-decili_total_income_neto\[6,1\]
t_prag_top_total_income_filter_60<-filter(data2, NET_INCOME> t_prag_top_total_income_50, NET_INCOME<=t_prag_top_total_income_60)
t_prag_top_total_income_filter_60_tax<-sum(t_prag_top_total_income_filter_60$TAX)
t_tax_share_60<-((t_prag_top_total_income_filter_60_tax)/ZBIR_TOTAL_TAX)*100
t_prag_top_total_income_filter_60<-sum(t_prag_top_total_income_filter_60$NET_INCOME)
t_prag_top_total_income_filter_60a<-nrow(filter(data2, NET_INCOME> t_prag_top_total_income_50, NET_INCOME<=t_prag_top_total_income_60))
t_prag_top_total_income_60b<-((t_prag_top_total_income_filter_60)/ZBIR_TOTAL_NET_INCOME)*100
FINAL_CENTILE_TABLE60<-data.frame(cbind(t_prag_top_total_income_60,t_prag_top_total_income_filter_60,t_prag_top_total_income_filter_60a,t_prag_top_total_income_60b,t_prag_top_total_income_filter_60_tax , t_tax_share_60))
colnames(FINAL_CENTILE_TABLE60)<-c("Decile threshold","Total income in the decile","Number of persons in the centile","Share of the decile in total income (%)","Tax","Share tax(%)")
FINAL_DECILE_TABLE <- rbind(FINAL_DECILE_TABLE, FINAL_CENTILE_TABLE60)
#DECILE 7
t_prag_top_total_income_70<-decili_total_income_neto\[7,1\]
t_prag_top_total_income_filter_70<-filter(data2, NET_INCOME> t_prag_top_total_income_60, NET_INCOME<=t_prag_top_total_income_70)
t_prag_top_total_income_filter_70_tax<-sum(t_prag_top_total_income_filter_70$TAX)
t_tax_share_70<-((t_prag_top_total_income_filter_70_tax)/ZBIR_TOTAL_TAX)*100
t_prag_top_total_income_filter_70<-sum(t_prag_top_total_income_filter_70$NET_INCOME)
t_prag_top_total_income_filter_70a<-nrow(filter(data2, NET_INCOME> t_prag_top_total_income_60, NET_INCOME<=t_prag_top_total_income_70))
t_prag_top_total_income_70b<-((t_prag_top_total_income_filter_70)/ZBIR_TOTAL_NET_INCOME)*100
FINAL_CENTILE_TABLE70<-data.frame(cbind(t_prag_top_total_income_70,t_prag_top_total_income_filter_70,t_prag_top_total_income_filter_70a,t_prag_top_total_income_70b,t_prag_top_total_income_filter_70_tax , t_tax_share_70))
colnames(FINAL_CENTILE_TABLE70)<-c("Decile threshold","Total income in the decile","Number of persons in the centile","Share of the decile in total income (%)","Tax","Share tax(%)")
FINAL_DECILE_TABLE <- rbind(FINAL_DECILE_TABLE, FINAL_CENTILE_TABLE70)
#DECILE 8
t_prag_top_total_income_80<-decili_total_income_neto\[8,1\]
t_prag_top_total_income_filter_80<-filter(data2, NET_INCOME> t_prag_top_total_income_70, NET_INCOME<=t_prag_top_total_income_80)
t_prag_top_total_income_filter_80_tax<-sum(t_prag_top_total_income_filter_80$TAX)
t_tax_share_80<-((t_prag_top_total_income_filter_80_tax)/ZBIR_TOTAL_TAX)*100
t_prag_top_total_income_filter_80<-sum(t_prag_top_total_income_filter_80$NET_INCOME)
t_prag_top_total_income_filter_80a<-nrow(filter(data2, NET_INCOME> t_prag_top_total_income_70, NET_INCOME<=t_prag_top_total_income_80))
t_prag_top_total_income_80b<-((t_prag_top_total_income_filter_80)/ZBIR_TOTAL_NET_INCOME)*100
FINAL_CENTILE_TABLE80<-data.frame(cbind(t_prag_top_total_income_80,t_prag_top_total_income_filter_80,t_prag_top_total_income_filter_80a,t_prag_top_total_income_80b,t_prag_top_total_income_filter_80_tax , t_tax_share_80))
colnames(FINAL_CENTILE_TABLE80)<-c("Decile threshold","Total income in the decile","Number of persons in the centile","Share of the decile in total income (%)","Tax","Share tax(%)")
FINAL_DECILE_TABLE <- rbind(FINAL_DECILE_TABLE, FINAL_CENTILE_TABLE80)
#DECILE 9
t_prag_top_total_income_90<-decili_total_income_neto\[9,1\]
t_prag_top_total_income_filter_90<-filter(data2, NET_INCOME> t_prag_top_total_income_80, NET_INCOME<=t_prag_top_total_income_90)
t_prag_top_total_income_filter_90_tax<-sum(t_prag_top_total_income_filter_90$TAX)
t_tax_share_90<-((t_prag_top_total_income_filter_90_tax)/ZBIR_TOTAL_TAX)*100
t_prag_top_total_income_filter_90<-sum(t_prag_top_total_income_filter_90$NET_INCOME)
t_prag_top_total_income_filter_90a<-nrow(filter(data2, NET_INCOME> t_prag_top_total_income_80, NET_INCOME<=t_prag_top_total_income_90))
t_prag_top_total_income_90b<-((t_prag_top_total_income_filter_90)/ZBIR_TOTAL_NET_INCOME)*100
FINAL_CENTILE_TABLE90<-data.frame(cbind(t_prag_top_total_income_90,t_prag_top_total_income_filter_90,t_prag_top_total_income_filter_90a,t_prag_top_total_income_90b,t_prag_top_total_income_filter_90_tax , t_tax_share_90))
colnames(FINAL_CENTILE_TABLE90)<-c("Decile threshold","Total income in the decile","Number of persons in the centile","Share of the decile in total income (%)","Tax","Share tax(%)")
FINAL_DECILE_TABLE <- rbind(FINAL_DECILE_TABLE, FINAL_CENTILE_TABLE90)
#DECILE 10
t_prag_top_total_income_100<-decili_total_income_neto\[10,1\]
t_prag_top_total_income_filter_100<-filter(data2, NET_INCOME> t_prag_top_total_income_90, NET_INCOME<=t_prag_top_total_income_100)
t_prag_top_total_income_filter_100_tax<-sum(t_prag_top_total_income_filter_100$TAX)
t_tax_share_100<-((t_prag_top_total_income_filter_100_tax)/ZBIR_TOTAL_TAX)*100
t_prag_top_total_income_filter_100<-sum(t_prag_top_total_income_filter_100$NET_INCOME)
t_prag_top_total_income_filter_100a<-nrow(filter(data2, NET_INCOME> t_prag_top_total_income_90, NET_INCOME<=t_prag_top_total_income_100))
t_prag_top_total_income_100b<-((t_prag_top_total_income_filter_100)/ZBIR_TOTAL_NET_INCOME)*100
FINAL_CENTILE_TABLE100<-data.frame(cbind(t_prag_top_total_income_100,t_prag_top_total_income_filter_100,t_prag_top_total_income_filter_100a,t_prag_top_total_income_100b,t_prag_top_total_income_filter_100_tax,t_tax_share_100))
colnames(FINAL_CENTILE_TABLE100)<-c("Decile threshold","Total income in the decile","Number of persons in the centile","Share of the decile in total income (%)","Tax","Share tax(%)")
FINAL_DECILE_TABLE <- rbind(FINAL_DECILE_TABLE, FINAL_CENTILE_TABLE100)
View(FINAL_DECILE_TABLE)][1]][1]