考虑到NA的情况,将for循环重写为sapply

时间:2020-01-16 12:31:18

标签: r for-loop if-statement sapply

我想让R为一定量的 "application/xml": { "schema": { "$ref": "#/components/schemas/ComponentaXML" } } 计算netincome

Income

现在,税金需要计算如下:

前五年(2001-2005年),收入<20 = 25%,收入> 20 == 50%

后五年(2006-2010),收入<15 = 20%,收入> 20 == 45%

我尝试将其编写如下:

panelID = c(1:50)   
year= c(2001:2010)
country = "NLD"
n <- 2
library(data.table)
set.seed(123)
DT <- data.table(panelID = rep(sample(panelID), each = n),
                 country = rep(sample(country, length(panelID), replace = T), each = n),
                 year = c(replicate(length(panelID), sample(year, n))),
                 some_NA = sample(0:5, 6),                                             
                 some_NA_factor = sample(0:5, 6),         
                 norm = round(runif(100)/10,2),
                 Income = round(rnorm(10,10,10),2),
                 Happiness = sample(10,10),
                 Sex = round(rnorm(10,0.75,0.3),2),
                 Age = sample(100,100),
                 Educ = round(rnorm(10,0.75,0.3),2))        
DT [, uniqueID := .I]                                                         # Creates a unique ID     
DT[DT == 0] <- NA 
DT$Income[DT$Income < 0] <- NA 
DT <- as.data.frame(DT)

但是我得到了错误:

for (i in DT$Income) {
    if (DT$Income[i] < 20 & DT$year[i] < 2006) {
        DT$netincome[i] <- DT$Income[i] - (DT$Income[i]*0.25)
    } else if (DT$Income[i] > 20 & DT$year[i] < 2006) {
        DT$netincome[i] <- DT$Income[i] - (20*0.25) - ((DT$Income[i]-20)*0.5)
    } else if (DT$Income[i] < 15 & DT$year[i] > 2005) {
        DT$netincome[i] <- DT$Income[i] - (DT$Income[i]*0.20)
    } else if (DT$Income[i] > 15 & DT$year[i] > 2005) {
        DT$netincome[i] <- DT$Income[i] - (15*0.20) - ((DT$Income[i]-15)*0.45)
    } 
    }

此外,我真的很想用Error in `$<-.data.frame`(`*tmp*`, "netincome", value = c(NA, NA, NA, : replacement has 15 rows, data has 100 用一种更简洁的方式来重写它,但是我在努力如何。

3 个答案:

答案 0 :(得分:2)

library(dplyr)
DT[Income < 0,Income:= NA] # better use this construction
DT[,.(netincome = case_when(Income < 20 & year < 2006 ~ Income - 0.25 * Income,
                            Income > 20 & year < 2006 ~ Income - 20 * 0.25 - 0.5 * (Income - 20),
                            Income < 15 & year > 2005 ~ Income - 0.2 * Income,
                            Income > 15 & year > 2005 ~ Income - 15*0.2 - 0.45 * (Income - 15)))]

如果使用一致的列名,这会容易得多(最佳实践请降低)。并尽量不要使用像DT这样的名称。 DT代表R中一种常用的软件包,这有点令人困惑。在未来的data.table版本中,会有一个fcase,它比case_when

答案 1 :(得分:2)

如果要在基数R中执行此操作,则无需使用sapply;您可以只嵌套一些ifelse语句。

DT$netincome <- with(DT, ifelse(year < 2006 & Income < 20, Income - (Income * 0.25),
  ifelse(year < 2006 & Income > 20, Income - (20 * 0.25) - ((Income - 20)* 0.5),
  ifelse(Income < 15, Income - (Income * 0.20), Income - (15 * 0.20) - ((Income - 15) * 0.45) ))))

结果列的摘要。这符合您的预期输出吗?

> summary(DT$netincome)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
  4.372   4.710  11.053  11.614  14.881  27.076      20 

答案 2 :(得分:0)

library(dplyr)
DT%>%
  mutate(netincome = case_when(Income < 20 & year < 2006 ~ Income - 0.25 * Income,
                               Income > 20 & year < 2006 ~ Income - 20*0.25 - 0.5*(Income-20),
                               Income < 15 & year > 2005 ~ Income -0.2*Income,
                               Income > 15 & year > 2005 ~ Income - 15*0.2 - 0.45*(Income-15))

如果您喜欢dplyr方法:),也可以使用%<>%此运算符。或者,如果您不想使用新列,则可以切换为汇总