forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbl_dt.Rd
67 lines (58 loc) · 1.65 KB
/
tbl_dt.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/tbl-dt.r
\name{tbl_dt}
\alias{.datatable.aware}
\alias{tbl_dt}
\title{Create a data table tbl.}
\usage{
tbl_dt(data, copy = TRUE)
}
\arguments{
\item{data}{a data table}
\item{copy}{If the input is a data.table, copy it?}
}
\description{
A data table tbl wraps a local data table.
}
\examples{
if (require("data.table")) {
ds <- tbl_dt(mtcars)
ds
as.data.table(ds)
as.tbl(mtcars)
}
if (require("data.table") && require("nycflights13")) {
flights2 <- tbl_dt(flights)
flights2 \%>\% filter(month == 1, day == 1, dest == "DFW")
flights2 \%>\% select(year:day)
flights2 \%>\% rename(Year = year)
flights2 \%>\%
summarise(
delay = mean(arr_delay, na.rm = TRUE),
n = length(arr_delay)
)
flights2 \%>\%
mutate(gained = arr_delay - dep_delay) \%>\%
select(ends_with("delay"), gained)
flights2 \%>\%
arrange(dest, desc(arr_delay))
by_dest <- group_by(flights2, dest)
filter(by_dest, arr_delay == max(arr_delay, na.rm = TRUE))
summarise(by_dest, arr = mean(arr_delay, na.rm = TRUE))
# Normalise arrival and departure delays by airport
by_dest \%>\%
mutate(arr_z = scale(arr_delay), dep_z = scale(dep_delay)) \%>\%
select(starts_with("arr"), starts_with("dep"))
arrange(by_dest, desc(arr_delay))
select(by_dest, -(day:tailnum))
rename(by_dest, Year = year)
# All manip functions preserve grouping structure, except for summarise
# which removes a grouping level
by_day <- group_by(flights2, year, month, day)
by_month <- summarise(by_day, delayed = sum(arr_delay > 0, na.rm = TRUE))
by_month
summarise(by_month, delayed = sum(delayed))
# You can also manually ungroup:
ungroup(by_day)
}
}