R 为什么';在这根管子里做什么?

R 为什么';在这根管子里做什么?,r,dplyr,rounding,R,Dplyr,Rounding,这是一个很烦人的问题,需要用一行额外的代码来解决,但我不明白为什么我试图按原样编写代码却不起作用 我试图在dplyr管道中对结果进行舍入,但舍入在最后不起作用,我不知道为什么(但我怀疑除法步骤是个问题) 以下是该问题的可复制示例: library(dplyr) # Use iris data set for reproducible example iris %>% select(Sepal.Length) %>% colSums()

这是一个很烦人的问题,需要用一行额外的代码来解决,但我不明白为什么我试图按原样编写代码却不起作用

我试图在dplyr管道中对结果进行舍入,但舍入在最后不起作用,我不知道为什么(但我怀疑除法步骤是个问题)

以下是该问题的可复制示例:

library(dplyr)

    # Use iris data set for reproducible example

    iris %>% 
      select(Sepal.Length) %>% 
      colSums() / 6 %>% 
      round()
输出:

Sepal.Length 
    146.0833 
Sepal.Length 
         146 
我希望输出为146,就像我这样做时一样(同样的事情,但将中间计算存储在变量“test”中):

round在dplyr管道中不起作用,无论我将其编码为
round(,0)
还是如上所述

似乎是
/6
打破了dplyr链,但我很想了解为什么以及如何在dplyr链中正确编写这一切


谢谢!

如果我们用
{}
包装,它就可以工作了

iris %>% 
  select(Sepal.Length) %>% 
  {colSums(.) / 6} %>%
  round
#   Sepal.Length 
#     146 

或者另一种选择是

iris %>% 
   select(Sepal.Length) %>%
   colSums %>% 
   divide_by(6) %>%
   round
#Sepal.Length 
#         146 

或者另一种方式是

iris %>%
   select(Sepal.Length) %>%
   colSums() %>% 
   `/`(6) %>% 
   round
#Sepal.Length 
#         146 

或使用
摘要

iris %>% 
    summarise(Sepal.Length = round(sum(Sepal.Length)/6))
#  Sepal.Length
#1          146

该问题与运算符优先级有关。根据
?语法

The following unary and binary operators are defined. They are listed in precedence groups, from highest to lowest.

:: :::  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)
定义了以下一元运算符和二元运算符。它们按优先级组从高到低列出。
::访问命名空间中的变量
$@组件/插槽提取
索引
^求幂(从右到左)
-+一元负号和加号
:序列运算符
%任何%的特殊操作员(包括%%和%/%)
*/乘,除
+-(二进制)加,减
<>=!=排序和比较
否定
&&&及
|| |或
~z~就像公式一样
->->>向右赋值

这里的问题是运算符优先级。
%>%
的运算符优先级高于
/
,因此
6%>%round()
首先执行
colSums()/6

运算符优先级在
?语法
页上给出

:: :::  access variables in a namespace
$ @ component / slot extraction
[ [[    indexing
^   exponentiation (right to left)
- + unary minus and plus
:   sequence operator
%any%   special operators (including %% and %/%) #This also includes pipes %>%
* / multiply, divide #division is after pipes
+ - (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)

为什么这些括号需要Akrun?我怀疑是这样的,但我尝试了括号,但它不起作用
:: :::  access variables in a namespace
$ @ component / slot extraction
[ [[    indexing
^   exponentiation (right to left)
- + unary minus and plus
:   sequence operator
%any%   special operators (including %% and %/%) #This also includes pipes %>%
* / multiply, divide #division is after pipes
+ - (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)
iris %>% 
  select(Sepal.Length) %>% 
  {colSums(.) / 6} %>%  
  round()

#Sepal.Length 
#         146