我正在学习r,目前正在查看magrittr
包中的管道运算符%>%。当我尝试以下操作时:
pi/2 %>% sin
结果为3.454967
,不正确
当我这样做时:
(pi/2) %>% sin
结果为1
,这是正确的。
从文档here看,我很好奇第一种情况。我找不到任何有用的东西。有人可以指出我的理解方向吗?
答案 0 :(得分:3)
撤消功能的顺序取决于?Syntax
帮助页面中描述的运算符优先级规则
:: ::: access variables in a namespace
$ @ component / slot extraction
[ [[ indexing
^ exponentiation (right to left)
- + unary minus and plus
: sequence operator
%any% special operators (including %% and %/%)
* / multiply, divide
+ - (binary) add, subtract
< > <= >= == != ordering and comparison
! negation
& && and
| || or
~ as in formulae
-> ->> rightwards assignment
<- <<- assignment (right to left)
= assignment (right to left)
? help (unary and binary)
请注意,%%
个函数的优先级比/
高,因此它们首先运行。所以本质上您正在跑步
pi/(2 %>% sin)
# pi/0.9092974
# [1] 3.454967