我不能使用“或带有mutate()的循环。警告在1:yrs中:数值表达式包含6个元素:仅第一个使用

时间:2019-12-18 15:12:35

标签: r for-loop dplyr iteration mutate

为某些计算创建了一个函数,该函数本身可以正常工作。 但是,当我在mutate()中使用它时,似乎将列中的所有值都作为输入并以警告消息结尾:
In 1:yrs : numerical expression has 6 elements: only the first used

library(dplyr)

mortgage <- function(amt = 800000, yrs = 25, mthly = 3630,  ints = 0.026) {
  yearPay <- mthly * 12
  paid <- 0
  remain <- amt
  for(i in 1:yrs) {
    paid = paid + yearPay
    remain = (amt - paid) * (1 + ints)
  }
  return(remain*-1)
}


tibble(loanAmt = c(800000, 800000, 800000, 700000, 700000, 700000),
                    period = c(15,20,25,15,20,25),
                    monthly = c(5373, 4279, 3630, 4701, 3744, 3176),
                    interest = 0.026) %>%
  mutate(credit = mortgage(loanAmt, period, monthly, interest))

我正在尝试对每行的数据运行函数mortgage(),但是mutate()似乎会将period中的所有值作为向量引入参数。
有人可以告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

对于此特定问题,您可以使用dplyr::rowwise

x1=tibble(loanAmt = c(800000, 800000, 800000, 700000, 700000, 700000),
       period = c(15,20,25,15,20,25),
       monthly = c(5373, 4279, 3630, 4701, 3744, 3176),
       interest = 0.026) %>%
  rowwise %>% 
  mutate(credit = mortgage(loanAmt, period, monthly, interest))
x1

这是因为for循环未向量化。不过,请记住,使用rowwise是按每行分组的,因此,如果您的数据集很大,则会很慢。

但是,您的函数似乎可以简化为该函数,不需要for循环:

mortgage2 <- function(amt, yrs, mthly,  ints) {
  remain = (amt - mthly * 12 * (yrs)) * (1 + ints)
  return(remain*-1)
}

x2=tibble(loanAmt = c(800000, 800000, 800000, 700000, 700000, 700000),
       period = c(15,20,25,15,20,25),
       monthly = c(5373, 4279, 3630, 4701, 3744, 3176),
       interest = 0.026) %>%
  mutate(credit = mortgage2(loanAmt, period, monthly, interest))
x2
all(x1$credit==x2$credit) # TRUE :-)

因此,那么不需要rowwise