> dim(flights)
[1] 336776 19
> head(flights, 3)
# A tibble: 3 x 19
year month day dep_time sched_dep_time dep_delay arr_time
<int> <int> <int> <int> <int> <dbl> <int>
1 2013 1 1 517 515 2 830
2 2013 1 1 533 529 4 850
3 2013 1 1 542 540 2 923
# ... with 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
# carrier <chr>, flight <int>, tailnum <chr>, origin <chr>,
# dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
# minute <dbl>, time_hour <dttm>
> flights_df <- tbl_df(flights)
> filter(flights_df, month == 1, day == 1)
# A tibble: 842 x 19
year month day dep_time sched_dep_time dep_delay arr_time
<int> <int> <int> <int> <int> <dbl> <int>
1 2013 1 1 517 515 2 830
2 2013 1 1 533 529 4 850
3 2013 1 1 542 540 2 923
4 2013 1 1 544 545 -1 1004
5 2013 1 1 554 600 -6 812
6 2013 1 1 554 558 -4 740
7 2013 1 1 555 600 -5 913
8 2013 1 1 557 600 -3 709
9 2013 1 1 557 600 -3 838
10 2013 1 1 558 600 -2 753
# ... with 832 more rows, and 12 more variables: sched_arr_time <int>,
# arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
# origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
# hour <dbl>, minute <dbl>, time_hour <dttm>
> > slice(flights_df, 1:10)
# A tibble: 10 x 19
year month day dep_time sched_dep_time dep_delay arr_time
<int> <int> <int> <int> <int> <dbl> <int>
1 2013 1 1 517 515 2 830
2 2013 1 1 533 529 4 850
3 2013 1 1 542 540 2 923
4 2013 1 1 544 545 -1 1004
5 2013 1 1 554 600 -6 812
6 2013 1 1 554 558 -4 740
7 2013 1 1 555 600 -5 913
8 2013 1 1 557 600 -3 709
9 2013 1 1 557 600 -3 838
10 2013 1 1 558 600 -2 753
# ... with 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
# carrier <chr>, flight <int>, tailnum <chr>, origin <chr>,
# dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
# minute <dbl>, time_hour <dttm>
> arrange(flights_df, year, month, day)
# A tibble: 336,776 x 19
year month day dep_time sched_dep_time dep_delay arr_time
<int> <int> <int> <int> <int> <dbl> <int>
1 2013 1 1 517 515 2 830
2 2013 1 1 533 529 4 850
3 2013 1 1 542 540 2 923
4 2013 1 1 544 545 -1 1004
5 2013 1 1 554 600 -6 812
6 2013 1 1 554 558 -4 740
7 2013 1 1 555 600 -5 913
8 2013 1 1 557 600 -3 709
9 2013 1 1 557 600 -3 838
10 2013 1 1 558 600 -2 753
# ... with 336,766 more rows, and 12 more variables:
# sched_arr_time <int>, arr_delay <dbl>, carrier <chr>, flight <int>,
# tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
# distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
> arrange(flights_df, year, desc(month), day)
# A tibble: 336,776 x 19
year month day dep_time sched_dep_time dep_delay arr_time
<int> <int> <int> <int> <int> <dbl> <int>
1 2013 12 1 13 2359 14 446
2 2013 12 1 17 2359 18 443
3 2013 12 1 453 500 -7 636
4 2013 12 1 520 515 5 749
5 2013 12 1 536 540 -4 845
6 2013 12 1 540 550 -10 1005
7 2013 12 1 541 545 -4 734
8 2013 12 1 546 545 1 826
9 2013 12 1 549 600 -11 648
10 2013 12 1 550 600 -10 825
# ... with 336,766 more rows, and 12 more variables:
# sched_arr_time <int>, arr_delay <dbl>, carrier <chr>, flight <int>,
# tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
# distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
> sample_n(flights_df, 10)
# A tibble: 10 x 19
year month day dep_time sched_dep_time dep_delay arr_time
<int> <int> <int> <int> <int> <dbl> <int>
1 2013 11 29 1551 1546 5 1756
2 2013 6 4 739 750 -11 1213
3 2013 3 5 1908 1905 3 2209
4 2013 7 17 611 615 -4 802
5 2013 10 9 755 810 -15 928
6 2013 3 12 608 615 -7 740
7 2013 9 25 1354 1400 -6 1508
8 2013 8 22 1848 1629 139 2136
9 2013 8 20 1426 1420 6 1631
10 2013 11 4 1349 1400 -11 1627
# ... with 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
# carrier <chr>, flight <int>, tailnum <chr>, origin <chr>,
# dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
# minute <dbl>, time_hour <dttm>
>
> sample_frac(flights_df, 0.01)
# A tibble: 3,368 x 19
year month day dep_time sched_dep_time dep_delay arr_time
<int> <int> <int> <int> <int> <dbl> <int>
1 2013 10 19 705 710 -5 1013
2 2013 5 29 1714 1648 26 1933
3 2013 2 6 756 759 -3 956
4 2013 5 30 2142 2145 -3 32
5 2013 12 28 620 605 15 746
6 2013 11 8 1907 1909 -2 2029
7 2013 7 15 624 625 -1 731
8 2013 10 5 1813 1815 -2 2100
9 2013 3 23 1244 1247 -3 1441
10 2013 4 8 1541 1545 -4 1818
# ... with 3,358 more rows, and 12 more variables: sched_arr_time <int>,
# arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
# origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
# hour <dbl>, minute <dbl>, time_hour <dttm>
>
> by_tailnum <- group_by(flights, tailnum)
> #确定组别为tailnum,赋值为by_tailnum
> delay <- summarise(by_tailnum,
count = n(),
dist = mean(distance, na.rm = TRUE),
delay = mean(arr_delay, na.rm = TRUE))
> #汇总flights里地tailnum组的分类数量,及其组别对应的distance和arr_delay的均值
> delay <- filter(delay, count > 20, dist < 2000)
> ggplot(delay, aes(dist, delay)) +
+ geom_point(aes(size = count), alpha = 1/2) +
+ geom_smooth() +
+ scale_size_area()
`geom_smooth()` using method = 'gam'
#汇总flights里地tailnum组的分类数量,及其组别对应的distance和arr_delay的均值
delay <- filter(delay, count > 20, dist < 2000)
ggplot(delay, aes(dist, delay)) +
geom_point(aes(size = count), alpha = 1/2) +
geom_smooth() +
scale_size_area()
a1 <- group_by(flights, year, month, day)
a2=tbl_df(a1)
a3<- select(a2, arr_delay, dep_delay)
a4<- summarise(a3,
arr = mean(arr_delay, na.rm = TRUE),
dep = mean(dep_delay, na.rm = TRUE))
> a4
# A tibble: 1 x 2
arr dep
<dbl> <dbl>
1 6.9 13
>
library(caret)
library(tidyr)
library(dplyr)
head(mtcars)
mtcarsnew=mtcars%>%gather(attribute,value,-carb)
> mtcarsnew
carb attribute value
1 4 mpg 21
2 4 mpg 21
3 1 mpg 22.8
4 1 mpg 21.4
5 2 mpg 18.7
6 1 mpg 18.1
7 4 mpg 14.3
8 2 mpg 24.4
9 2 mpg 22.8
10 4 mpg 19.2
11 4 mpg 17.8
12 3 mpg 16.4
13 3 mpg 17.3
14 3 mpg 15.2
15 4 mpg 10.4
16 4 mpg 10.4
17 4 mpg 14.7
18 1 mpg 32.4
19 2 mpg 30.4
20 1 mpg 33.9
21 1 mpg 21.5
22 2 mpg 15.5
23 2 mpg 15.2
mtcarsnew1=mtcars%>%gather(attribute,value,mpg:gear)
> mtcarsnew1
carb transmission attribute value
1 4 Manual mpg 21
2 4 Manual mpg 21
3 1 Manual mpg 22.8
4 1 Automatic mpg 21.4
5 2 Automatic mpg 18.7
6 1 Automatic mpg 18.1
7 4 Automatic mpg 14.3
8 2 Automatic mpg 24.4
9 2 Automatic mpg 22.8
10 4 Automatic mpg 19.2
11 4 Automatic mpg 17.8
12 3 Automatic mpg 16.4
13 3 Automatic mpg 17.3
14 3 Automatic mpg 15.2
15 4 Automatic mpg 10.4
16 4 Automatic mpg 10.4
17 4 Automatic mpg 14.7
18 1 Manual mpg 32.4
19 2 Manual mpg 30.4
20 1 Manual mpg 33.9
21 1 Automatic mpg 21.5
22 2 Automatic mpg 15.5
23 2 Automatic mpg 15.2
24 4 Automatic mpg 13.3
25 2 Automatic mpg 19.2
26 1 Manual mpg 27.3
27 2 Manual mpg 26
28 2 Manual mpg 30.4
29 4 Manual mpg 15.8
> str(mtcars)
'data.frame': 32 obs. of 12 variables:
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
$ disp : num 160 160 108 258 360 ...
$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$ drat : num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ wt : num 2.62 2.88 2.32 3.21 3.44 ...
$ qsec : num 16.5 17 18.6 19.4 17 ...
$ vs : Factor w/ 2 levels "V-Engine","Straight Engine": 1 1 2 2 1 2 1 2 2 2 ...
$ am : Factor w/ 2 levels "Automatic","Manual": 2 2 2 1 1 1 1 1 1 1 ...
$ gear : num 4 4 4 3 3 3 3 4 4 4 ...
$ carb : num 4 4 1 1 2 1 4 2 2 4 ...
$ transmission: Factor w/ 2 levels "Automatic","Manual": 2 2 2 1 1 1 1 1 1 1 ...
>
date=as.Date("2017-01-01")+0:14
hour=sample(1:24,15)
min=sample(1:60,15)
data=data.frame(date,hour,min)
> data
date hour min
1 2017-01-01 19 11
2 2017-01-02 2 45
3 2017-01-03 7 12
4 2017-01-04 16 15
5 2017-01-05 11 56
6 2017-01-06 3 59
7 2017-01-07 10 30
8 2017-01-08 9 35
9 2017-01-09 13 17
10 2017-01-10 24 32
11 2017-01-11 12 52
12 2017-01-12 14 25
13 2017-01-13 1 33
14 2017-01-14 4 23
15 2017-01-15 21 58
>
datanew=data%>%unite(datehour,date,hour,sep=" ")%>%unite(datetime,datehour,min,sep=":")
> datanew
datetime
1 2017-01-01 19:11
2 2017-01-02 2:45
3 2017-01-03 7:12
4 2017-01-04 16:15
5 2017-01-05 11:56
6 2017-01-06 3:59
7 2017-01-07 10:30
8 2017-01-08 9:35
9 2017-01-09 13:17
10 2017-01-10 24:32
11 2017-01-11 12:52
12 2017-01-12 14:25
13 2017-01-13 1:33
14 2017-01-14 4:23
15 2017-01-15 21:58
data1=datanew%>%separate(datetime,c("date","time"),sep=" ")%>%separate(time,c("hour","min"),sep=":")
> data1
date hour min
1 2017-01-01 19 11
2 2017-01-02 2 45
3 2017-01-03 7 12
4 2017-01-04 16 15
5 2017-01-05 11 56
6 2017-01-06 3 59
7 2017-01-07 10 30
8 2017-01-08 9 35
9 2017-01-09 13 17
10 2017-01-10 24 32
11 2017-01-11 12 52
12 2017-01-12 14 25
13 2017-01-13 1 33
14 2017-01-14 4 23
15 2017-01-15 21 58
>
mtcarMelt=melt(mtcars,id.vars=c("cyl","gear"),variable.name="carvariable",value.name="carvalue")
> mtcarMelt
cyl gear carvariable carvalue
1 6 4 mpg 21
2 6 4 mpg 21
3 4 4 mpg 22.8
4 6 3 mpg 21.4
5 8 3 mpg 18.7
6 6 3 mpg 18.1
7 8 3 mpg 14.3
8 4 4 mpg 24.4
9 4 4 mpg 22.8
10 6 4 mpg 19.2
11 6 4 mpg 17.8
12 8 3 mpg 16.4
13 8 3 mpg 17.3
14 8 3 mpg 15.2
15 8 3 mpg 10.4
16 8 3 mpg 10.4
17 8 3 mpg 14.7
18 4 4 mpg 32.4
19 4 4 mpg 30.4
20 4 4 mpg 33.9
21 4 3 mpg 21.5
22 8 3 mpg 15.5
23 8 3 mpg 15.2
24 8 3 mpg 13.3
25 8 3 mpg 19.2
26 4 4 mpg 27.3
27 4 5 mpg 26
28 4 5 mpg 30.4
29 8 5 mpg 15.8
30 6 5 mpg 19.7
31 8 5 mpg 15
32 4 4 mpg 21.4
33 6 4 disp 160