%>%来自dplyr包的管道函数,其作用是将前一步的结果直接传参给下一步的函数,从而省略了中间的赋值步骤,可以大量减少内存中的对象,节省内存
%>%,该符号将左边的对象作为第一个参数传递到右边的函数中
直接上栗子??: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
时间: 2024-12-21 19:17:51