有没有更优雅的方法将0到1之间的x投影到w形状?

Ren*_*ger 3 r projection

我需要将x0到1之间的值()投影到aw shape(y=f(x)):

在此输入图像描述

我可以通过以下功能实现我所需要的:

f <- function(x) {
  if (x < 0.25) {
    return (1-4*x)
  }
  if (x < 0.50) {
    return (-1 + 4*x)
  }
  if (x < 0.75) {
    return (3 - 4*x)
  }
  return (-3 + 4*x)
}


x <- 0:20/20
y <- lapply(x, f)

plot(x, y)
lines(x,y, col='red')
Run Code Online (Sandbox Code Playgroud)

但是,我倾向于相信我的问题有一个更优雅的解决方案,可能是一个班轮.

R中有这样的东西吗?

Hon*_*Ooi 6

f <- function(x)
abs(abs(2 - 4 * x) - 1)

plot(f)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述