根据 R 中的条件修改值

Modify values based on condition in R

我有以下数据框:

x   income

1   46200           
2   89345           
3   189982          
4   255465          
5   301189          
6   18100           
7   55234           
8   900672          
9   108221          
10  22201

我正在尝试根据每个值所在的范围(例如 0-20k、20-50k、50-90k、90-140k 和 >140k)对每个值应用不同的函数。这个函数看起来像:

平均税率=((先前边际税率+((收入-当前税率下限)*边际税率))/收入

我曾尝试在 if 和 else if 条件下使用 for 循环,但是我在将其应用于我的 df 时收效甚微,我不确定在执行所需操作时我的替代方案是什么。我也不确定哪种 R 函数最适合这种操作。非常感谢任何帮助,因为 R 对我来说是新手。

编辑

Tax schedule & prior taxable amounts

可重现的例子:

收入=60,000

平均税 = ((6000 + ((60000 - 50000)*0.35))/60000

平均税收 = 15.83%

这是更友好的可重现格式的数据,在最后一行添加了给定的具体示例

dd <- read.table(text="
x   income
1   46200           
2   89345           
3   189982          
4   255465          
5   301189          
6   18100           
7   55234           
8   900672          
9   108221          
10  22201
11  60000", header=T)

首先,将您的税务数据从图像中移出并移至 table

taxinfo <- read.table(text="
min max rate prior
0 20000 0 0
20000 50000 .2 0
50000 90000 .35 6000
90000 140000 .4 20000
140000 Inf .5 40000", header=TRUE)

现在我们可以使用每个括号的行索引更轻松地从此 table 中提取相关信息。我们可以使用 cut()

轻松找到每个人的括号
dd$bracket <- cut(dd$income, breaks=c(-Inf, taxinfo$max),labels = FALSE)

我们使用 taxinfo table 中的最大值来创建中断。我们将此值分配给 table 中的新列。现在我们知道了这个人所在的每个括号的索引。我们可以使用该信息对 taxinfo table 进行索引以进行计算

dd$avgtax <- with(taxinfo, 
  (prior[dd$bracket] + (dd$income-min[dd$bracket])*rate[dd$bracket]) / dd$income
)

priorminrate 的值来自 taxinfo。我们使用括号值来索引每个组的值的列。我们也将其分配给一个新的数据列。这是输出

    x income bracket     avgtax
1   1  46200       2 0.11341991
2   2  89345       3 0.22128547
3   3 189982       5 0.34209030
4   4 255465       5 0.38256708
5   5 301189       5 0.40039477
6   6  18100       1 0.00000000
7   7  55234       3 0.14179491
8   8 900672       5 0.46669154
9   9 108221       4 0.25215439
10 10  22201       2 0.01982794
11 11  60000       3 0.15833333

这对于 dplyr 包来说相对简单。首先,这里有一些您的示例数据,我们可以将其用作示例:

df <- data.frame(
  x = c(1, 2, 3, 4, 5, 6),
  income = c(46200, 89345, 189982, 255465, 301189, 60000)
)
df

输出:

  x income
1 1  46200
2 2  89345
3 3 189982
4 4 255465
5 5 301189
6 6  60000

现在,我们可以导出我们需要的其他值并执行最终计算:

library(dplyr)

df %>% 
  mutate(
    marginal_rate = case_when(
      income < 20000 ~ 0, 
      income >= 20000 & income < 50000 ~ .2,
      income >= 50000 & income < 90000 ~ .35,
      income >= 90000 & income < 140000 ~ .4,
      income >= 140000 ~ .5
    ), 
    prior_taxable_amount = case_when(
      income < 50000 ~ 0, 
      income >= 50000 & income < 90000 ~ 6000,
      income >= 90000 & income < 140000 ~ 20000,
      income >= 140000 ~ 40000
    ), 
    current_bracket_lower_bound = case_when(
      income < 20000 ~ 0, 
      income >= 20000 & income < 50000 ~ 20000,
      income >= 50000 & income < 90000 ~ 50000,
      income >= 90000 & income < 140000 ~ 90000,
      income >= 140000 ~ 140000
    ), 
    avgtax = 
      (prior_taxable_amount + 
      ((income - current_bracket_lower_bound) * 
          marginal_rate)) / income
  ) 

输出:

  x income marginal_rate prior_taxable_amount current_bracket_lower_bound    avgtax
1 1  46200          0.20                    0                       20000 0.1134199
2 2  89345          0.35                 6000                       50000 0.2212855
3 3 189982          0.50                40000                      140000 0.3420903
4 4 255465          0.50                40000                      140000 0.3825671
5 5 301189          0.50                40000                      140000 0.4003948
6 6  60000          0.35                 6000                       50000 0.1583333