%>%来自dplyr包的管道函数,其作用是将前一步的结果直接传参给下一步的函数,从而省略了中间的赋值步骤,可以大量减少内存中的对象,节省内存
符号%>%
,这是管道操作,其意思是将%>%
左边的对象传递给右边的函数,作为第一个选项的设置(或剩下唯一一个选项的设置)
x %>% f(y) 等同于 f(x, y)
y %>% f(x, ., z) 等同于 f(x, y, z )
直接上栗子?:
library(data.table)
## date hour min second event
## 1: 2016-11-01 7 30 29 u
## 2: 2016-11-02 9 43 36 a
typeof(data)
## [1] "list"
------------------------------------------管道函数(%>%)很简单-------------------------
## datetime event
## 1: 2016-11-01 7:30:29 u
## 2: 2016-11-02 9:43:36 a
## 3: 2016-11-03 13:58:60 l
## 4: 2016-11-04 20:22:11 q
## 5: 2016-11-05 5:44:47 p
## 6: 2016-11-06 18:52:37 k
## 7: 2016-11-07 19:12:43 r
## 8: 2016-11-08 12:35:6 i
## 9: 2016-11-09 11:7:38 e
## 10: 2016-11-10 1:14:21 b
## 11: 2016-11-11 3:20:42 w
## 12: 2016-11-12 14:1:32 t
## 13: 2016-11-13 23:19:52 h
## 14: 2016-11-14 21:41:26 s
## 15: 2016-11-15 8:16:25 o
(%>%) 分步写
## datehour min second event
## 1: 2016-11-01 7 30 29 u
## 2: 2016-11-02 9 43 36 a
## 3: 2016-11-03 13 58 60 l
## 4: 2016-11-04 20 22 11 q
## 5: 2016-11-05 5 44 47 p
## 6: 2016-11-06 18 52 37 k
## 7: 2016-11-07 19 12 43 r
## 8: 2016-11-08 12 35 6 i
## 9: 2016-11-09 11 7 38 e
## 10: 2016-11-10 1 14 21 b
## 11: 2016-11-11 3 20 42 w
## 12: 2016-11-12 14 1 32 t
## 13: 2016-11-13 23 19 52 h
## 14: 2016-11-14 21 41 26 s
## 15: 2016-11-15 8 16 25 o
dataNew %>% unite(datetime, datehour, min, second, sep = ':')
## datetime event
## 1: 2016-11-01 7:30:29 u
## 2: 2016-11-02 9:43:36 a
## 3: 2016-11-03 13:58:60 l
## 4: 2016-11-04 20:22:11 q
## 5: 2016-11-05 5:44:47 p
## 6: 2016-11-06 18:52:37 k
## 7: 2016-11-07 19:12:43 r
## 8: 2016-11-08 12:35:6 i
## 9: 2016-11-09 11:7:38 e
## 10: 2016-11-10 1:14:21 b
## 11: 2016-11-11 3:20:42 w
## 12: 2016-11-12 14:1:32 t
## 13: 2016-11-13 23:19:52 h
## 14: 2016-11-14 21:41:26 s
## 15: 2016-11-15 8:16:25 o
## datehour min second event
## 1: 2016-11-01 7 30 29 u
## 2: 2016-11-02 9 43 36 a
## 3: 2016-11-03 13 58 60 l
## 4: 2016-11-04 20 22 11 q
## 5: 2016-11-05 5 44 47 p
## 6: 2016-11-06 18 52 37 k
## 7: 2016-11-07 19 12 43 r
## 8: 2016-11-08 12 35 6 i
## 9: 2016-11-09 11 7 38 e
## 10: 2016-11-10 1 14 21 b
## 11: 2016-11-11 3 20 42 w
## 12: 2016-11-12 14 1 32 t
## 13: 2016-11-13 23 19 52 h
## 14: 2016-11-14 21 41 26 s
## 15: 2016-11-15 8 16 25 o
dt2<-unite(dt,datatime,datehour, min, second, sep = ':') dt2
## datatime event
## 1: 2016-11-01 7:30:29 u
## 2: 2016-11-02 9:43:36 a
## 3: 2016-11-03 13:58:60 l
## 4: 2016-11-04 20:22:11 q
## 5: 2016-11-05 5:44:47 p
## 6: 2016-11-06 18:52:37 k
## 7: 2016-11-07 19:12:43 r
## 8: 2016-11-08 12:35:6 i
## 9: 2016-11-09 11:7:38 e
## 10: 2016-11-10 1:14:21 b
## 11: 2016-11-11 3:20:42 w
## 12: 2016-11-12 14:1:32 t
## 13: 2016-11-13 23:19:52 h
## 14: 2016-11-14 21:41:26 s
## 15: 2016-11-15 8:16:25 o