我有以下数据:
library(tidyverse)
table <- tibble(
points = 0:10,
energy = c(0, 120, 240, 300, 380, 500, 600, 780, NA, NA, NA),
cost = c(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
)
我正在try 编写一个函数,该函数将一个值作为参数,对照energy
中的值进行测量,然后返回关联的points
值.
到目前为止,我可以理解一个基本的机制来提取它,大致如下:
energy_points <- function(energy_input) {
table_one %>%
filter(energy == energy_input) %>%
pull(points)
}
然而,我的函数的预期操作是,在分配分数之前,它查看Energy_input值并判断它属于哪个"范围".
因此,值125大于120且小于241,则返回点数1.值120不大于120,因此返回点数0.一些更多的预期输出如果有帮助的话:
energy_input | points |
---|---|
50 | 0 |
120 | 0 |
121 | 1 |
240 | 1 |
241 | 2 |
How can I write the function so it checks ranges based on values in the table?个