我正在编写一个带有几个管道的函数.我想在最后一个管道之前将一些步骤保存为.tbl或数据框.例如:a %>% b %>% c,我想保存步骤'c',但也想要步骤'b'.
我知道一个选择是做两个管道,但我相信必须有更好的方法.
cars %>% mutate(kmh = dist/speed) %>% summary()
input = cbind(c(3,7,3,5,2,9,1,4,6,4,7,3,7,4))
library(zoo)
output = cbind(rollmean(input,4))
print(input)
print(output)
Run Code Online (Sandbox Code Playgroud)
输出:
[,1]
[1,] 3
[2,] 7
[3,] 3
[4,] 5
[5,] 2
[6,] 9
[7,] 1
[8,] 4
[9,] 6
[10,] 4
[11,] 7
[12,] 3
[13,] 7
[14,] 4
[,1]
[1,] 4.50
[2,] 4.25
[3,] 4.75
[4,] 4.25
[5,] 4.00
[6,] 5.00
[7,] 3.75
[8,] 5.25
[9,] 5.00
[10,] 5.25
[11,] 5.25
Run Code Online (Sandbox Code Playgroud)
但是当我试图解决它时:
Error in cbind(input, output) :
number of rows of matrices must match (see arg 2)
Calls: print …Run Code Online (Sandbox Code Playgroud)