我有两个向量组合起来创建data.frame
:
datax = data.frame(vlead = vlr, vfoll= vr)
> dput(datax)
structure(list(vlead = c(18, 18.1, 18.2, 18.3, 18.4, 18.5, 18.6,
18.7, 18.8, 18.9, 19, 19.1, 19.2, 19.3, 19.4, 19.5, 19.6, 19.7,
19.8, 19.9, 20, 20.1, 20.2, 20.3, 20.4, 20.5, 20.6, 20.7, 20.8,
20.9, 21, 21.1, 21.2, 21.3, 21.4, 21.5, 21.6, 21.7, 21.8, 21.9,
22, 22.1, 22.2, 22.3, 22.4, 22.5, 22.6, 22.7, 22.8, 22.9, 23,
23.1, 23.2), vfoll = c(20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20)), .Names = c("vlead",
"vfoll"), row.names = c(NA, -53L), class = "data.frame")
以下功能:
s_BX <- function(v,vl){
1.5 + 0.9*min(v,vl)
}
在datax
数据框中,我想创建一个名为BX
的新列,该列是函数s_BX
的结果,用于给定的vlead和vfoll:
library(dplyr)
> datax %>% mutate(BX = s_BX(vfoll, vlead))
vlead vfoll BX
1 18.0 20 17.7
2 18.1 20 17.7
3 18.2 20 17.7
4 18.3 20 17.7
5 18.4 20 17.7
6 18.5 20 17.7
7 18.6 20 17.7
8 18.7 20 17.7
9 18.8 20 17.7
10 18.9 20 17.7
该函数不是获取给定行的minimum
和vlead
,而是获取整个向量的vfoll
。
如何解决此问题以获得以下预期输出?
min
答案 0 :(得分:2)
您可以使用rowwise()
:
datax %>% rowwise() %>% mutate(BX = s_BX(vfoll, vlead))