管道函数(%>%)很简单

%>%来自dplyr包的管道函数,其作用是将前一步的结果直接传参给下一步的函数,从而省略了中间的赋值步骤,可以大量减少内存中的对象,节省内存

符号%>%,这是管道操作,其意思是将%>%左边的对象传递给右边的函数,作为第一个选项的设置(或剩下唯一一个选项的设置)

x %>% f(y) 等同于 f(x, y)
y %>% f(x, ., z) 等同于 f(x, y, z )
直接上栗子?:
library(data.table) library(tidyr) # 先虚构一数据框 set.seed(1) date <- as.Date('2016-11-01') + 0:14 hour <- sample(1:24, 15) min <- sample(1:60, 15) second <- sample(1:60, 15) event <- sample(letters, 15) data <- data.table(date, hour, min, second, event) head(data)[0:2]
##          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"

------------------------------------------管道函数(%>%)很简单-------------------------
# 把date,hour,min和second列合并为新列datetime
# R中的日期时间格式为"Year-Month-Day-Hour:Min:Second"
#(一)(%>%) 合着写
dataNew <- data %>%unite(datehour, date, hour, sep = ' ') %>%unite(datetime, datehour, min, second, sep = ':') dataNew
##                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
# #(二)(%>%) 分步写
dataNew <- data %>%unite(datehour, date, hour, sep = ' '); dataNew
##          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
# #(三)不用%>%也能写
dt<-unite(data,datehour, date, hour, sep = ' ') dt
##          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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值