简体   繁体   English

如何在 R 中使用链运算符进行自身计算?

[英]How can I use the chain operator do calculation with itself in R?

I have a function, where x is between 0 and 1:我有一个函数,其中 x 介于 0 和 1 之间:

在此处输入图片说明

How can I use the chain operator %>% from dplyr package to do it?如何使用dplyr包中的链运算符%>%来做到这一点?

I tried the following:我尝试了以下方法:

x = 0.5
y <- (x-1)^2 %>% 1-. %>% sqrt()

But this is wrong!但这是错误的! How can I find a way with the chain operator do calculation with itself?我怎样才能找到一种与链操作员一起进行计算的方法? I made here just a small example.我在这里只是一个小例子。 I have a long equation and want to made the code readable with chain operator.我有一个很长的等式,并希望使用链运算符使代码可读。

Promoting my comment to an answer as the OP appears to agree (and removing a spurious ) in the process):促进我的评论一个答案作为OP似乎同意(并取出假)的过程中):

computeY <- function(x) { sqrt(1-(x-1)^2) }

which at the prompt works as well:在提示符下也可以工作:

R> computeY <- function(x) { sqrt(1-(x-1)^2) }
R> sapply((1:5)/10, computeY)
[1] 0.435890 0.600000 0.714143 0.800000 0.866025
R> 

Disclaimer: No pipes were hurt in the production of this answer.免责声明:在此答案的制作过程中没有损坏管道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM