为某些计算创建了一个函数,该函数本身可以正常工作。
但是,当我在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
中的所有值作为向量引入参数。
有人可以告诉我我做错了什么吗?
答案 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
。