为方便处理,在数据集中增加一列car
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
gather-宽数据转为长数据,类似于reshape2包中的melt函数
mtcarsNew <- mtcars%>%gather(attribute,value,-car)
#tidyr很好的一点是可以只gather若干列而其他列保持不变。如果你想gather在map和gear之间的所有列而保持carb和car列不变
mtcarsNew <- mtcars%>%gather(attribute,value,mpg:gear)
spread—长数据转为宽数据,类似于reshape2包中的cast函数
mtcarsSpread <- mtcarsNew%>%spread(attribute,value)
unit—多列合并为一列
set.seed(1)
date <- as.Date('2016-01-01') + 0:14
hour <- sample(1:24, 15)
min <- sample(1:60, 15)
second <- sample(1:60, 15)
event <- sample(letters, 15)
data <- data.frame(date, hour, min, second, event)
dataNew <- data%>%
unite(datehour,date,hour,sep=' ')%>%
unite(datetime,datehour,min,second,sep=':')
separate—将一列分离为多列
data1 <- dataNew%>%
separate(datetime,c('date','time'),sep=' ')%>%
separate(time,c('hour','min','second'),sep=':')