reduce()和reduce_right()可以將list或是vector中的element依序由左而右,或是由右到左的處理,提供一些基本的數理處置。
可以參考下面的範例來使用reduce()
1:3 %>% reduce(`+`) #[1]6 1:10 %>% reduce(`*`) #[1]3628800 5 %>% replicate(sample(10, 5), simplify = FALSE) %>% reduce(intersect) #[1]7 #每次都不同,因為有sample這函數在裡面 x <- list(c(0, 1), c(2, 3), c(4, 5)) x %>% reduce(c) #[1] 0 1 2 3 4 5 x %>% reduce_right(c) #[1] 4 5 2 3 0 1 #可以用上面的例子來理解reduce()和reduce_right()兩者的差異