gpt4 book ai didi

r - "Top 20% of the people earn 80% of the money"...使用R的这种类型的结果

转载 作者:行者123 更新时间:2023-12-04 21:43:45 26 4
gpt4 key购买 nike

让我给你举个例子:

person | salary
----------------
1 | 30'000
2 | 10'000
3 | 15'000
4 | 25'000
5 | 80'000
6 | 56'000
... | ...

获得此结果的步骤是对工资进行排序,然后创建一个新表,给出从开始到相应行的行/人的份额以及从开始到相应行的工资总和的份额(工资总额)。

然后一个人只需要为人们选择最接近 20% 的那一行,我们就知道他们赚了多少。

这是一个非常标准的问题 - 但由于我不知道如何口头提及它,所以我无法用谷歌搜索它。

因此,如果有人能告诉我“称之为”什么以及如何在 R 中最简单地计算和绘制它,我将不胜感激 - 所以没有循环和东西。我的直觉告诉我,至少有 5 个包和 10 个函数可以解决这种情况。也许类似于具有固定分位数的 summary() 。

因此,让我们假设上表可用作数据框:
salaries <- data.frame(person = c(1,2,3,...), salary = c(30000,...))

最佳答案

使用 SLID来自 car 的收入数据集-包裹:

library(car)

dat <- SLID[!is.na(SLID$wage),] # Remove missing values
dat$income <- dat$wage*40*50 # "translate" the wages to their full time annual earnings equivalent.
dat$id <- seq(1,nrow(dat))

# Create a data.frame with a person ID and their annual income:
keep <- data.frame(id = seq(1, nrow(dat)),
income = dat$income)
keep <- keep[order(keep$income, decreasing = TRUE),] # Descending ordering according to income
keep$accum <- cumsum(keep$income) # Cumulative sum of the descending incomes
keep$pct <- keep$accum/sum(keep$income)*100 # % of the total income

keep$check <- keep$pct<80 # Check where the % is smaller than 80%
threshold <- min(which(keep$check == FALSE)) # First line where % is larger than 80%
border <- threshold/nrow(keep)*100 # Check which percentile that was
border <- round(border, digits = 2)
paste0(border, "% of the people earn 80% of the income")

#[1] "62.41% of the people earn 80% of the income"

正如我们所期望的那样,经典的 80-20 规则将显示“20% 的人赚取 80% 的收入”。如您所见,此规则不适用于此处。

反向论证:
# The 20% of the people earn X % of total income:

linenr <- ceiling(1/5*nrow(keep))
outcome2 <- round(keep$pct[linenr], digits = 2)
paste0(outcome2, "% of total income is earned by the top 20% of the people")

# [1] "36.07% of total income is earned by the top 20% of the people"

请注意,此处提供的数字并不代表现实世界:)

另外, Wikipedia有更多关于帕累托原则的信息,也称为 80-20 规则。似乎这条规则出现在多种环境中,例如商业、经济和数学。

关于r - "Top 20% of the people earn 80% of the money"...使用R的这种类型的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18679525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com