【问题标题】:How to solve: the condition has length > 1 and only the first element will be used如何解决:条件的长度> 1,并且只使用第一个元素
【发布时间】:2017-02-19 10:43:58
【问题描述】:

我使用以下代码使用 R 计算税金。其中两个变量可以作为参数给出。当使用数据集时,我想根据类别计算税款。我是包开发的新手。请帮我解决这个问题。

插入数据集时出现错误,仅计算第一个类别 并获取此 Waring 消息。

警告信息: 在 if (category == 1) { : 条件的长度 > 1,并且只使用第一个元素

IIT<- function(income,category) {
if (category == 1){
if (income > 0 && income <= 18200) {
tax <- 0
} else if (income > 18200 && income <= 37000) {
tax <- (income - 18200) * .10
} else if (income > 37000 && income <= 80000) {
tax <- 3572 + (income - 37000) * .20
} else if (income > 80000 && income <= 180000) {
tax <- 17547 + (income - 80000) * .30
} else if (income > 180000 && Inf) {
tax <- 54547 + (income - 180000) * .40
}
return(tax)}
else if (category==2){
if (income > 0 && income <= 18200) {
  tax <- 0
} else if (income > 18200 && income <= 37000) {
  tax <- (income - 18200) * .15
} else if (income > 37000 && income <= 80000) {
  tax <- 3572 + (income - 37000) * .25
} else if (income > 80000 && income <= 180000) {
  tax <- 17547 + (income - 80000) * .35
} else if (income > 180000 && Inf) {
  tax <- 54547 + (income - 180000) * .45
}
return(tax)
}
}

【问题讨论】:

  • 你向函数“传递”了什么?单个值或数组/数据框?
  • if (income &amp;&amp; category == 1) 应该做什么,你的意思是if (category == 1)
  • 让我们说 datset 有收入和类别,收入:25000,25000,30000,30000 类别:1,2,1,2 如程序 1 所述,有不同的税收和 2一套税收规则。
  • 我要数据帧。
  • 也适用于数据集。谢谢

标签: r if-statement r-package


【解决方案1】:

首先,尽量保持简单。你的语法比它需要的更复杂。对于一对值,您可以将函数改写为:

single.IIT <- function(income, category) {
    if (income < 0) stop("Error in IIT: income must bei > 0.")
    if (category == 1){
        if (income <= 18200) return(0)
        if (income <= 37000) return((income - 18200) * .19)
        if (income <= 80000) return(3572 + (income - 37000) * .325)
        if (income <= 180000) return(17547 + (income - 80000) * .37)
        return(54547 + (income - 180000) * .45)
        }

    if (category==2){
        if (income <= 18200) return(0)
        if (income <= 37000) return((income - 18200) * .15)
        if (income <= 80000) return(3572 + (income - 37000) * .25)
        if (income <= 180000) return(17547 + (income - 80000) * .35)
        return(54547 + (income - 180000) * .45)
    }

    stop("ERROR in IIT: category must be either 1 or 2.")
}

您可以在简短版本中更轻松地发现错误。由于您想一次处理多对数据,因此需要对其进行矢量化处理:

IIT <- Vectorize(single.IIT)

现在你可以测试它了:

> IIT( income = c(23000, 500000, 0), category = c(1, 2, 1))
[1]    912 198547      0
> IIT( income = c(0, 0, 500, 500, 19000, 19000, 40000, 40000),
+      category = c(1, 2, 1, 2, 1, 2, 1, 2))
[1]    0    0    0    0  152  120 4547 4322
> IIT( income = c(0, 18000, -20), category = c( 1, 1, 1))
Error in (function (income, category)  : 
Error in IIT: income must bei > 0.
> IIT( income = c(0, 18000, 202), category = c( 1, 1, 5))
Error in (function (income, category)  : 
ERROR in IIT: category must be either 1 or 2.

编辑:在 cmets 中,您询问如何将其与数据框一起使用:

expl <- data.frame(income = c(30000, 40000, 50000,60000),
                  bodyweight = c(75, 60, 45, 98),
                  nationality = c("F", "CH", "D", "AU"),
                  category = c(1, 2, 1, 2))
# we need the first and the fourth column in that dataframe
expl$tax <- IIT(expl[[1]], expl[[4]])
print(expl)
plot(tax ~ income, data = expl, col=category, pch=19)

【讨论】:

  • 非常感谢它工作得很好,但是当我导入数据框 frm 电子表格时。这不起作用。能说说怎么用吗?
  • 请不要写“不起作用”,但要说明什么不起作用。如果您想了解函数如何处理您的数据,请提供一些数据。这次,我编辑了我的答案以提供一个示例数据框以及如何使用它。
  • 很抱歉使用它。如果我将电子表格插入到 R 环境中,我现在有一个电子表格,其中一列带有“收入”,另一列带有“类别”。我只收到类别 1 的答案。它不会计算 Cat 2 的值。当 cat 在数据中被提及为 2 时。它仍然只计算并发送 1 的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
  • 2021-02-17
相关资源
最近更新 更多