dplyr (version 0.4.0.9000)

tbl_dt: Create a data table tbl.

Description

A data table tbl wraps a local data table.

Usage

tbl_dt(data, copy = TRUE)

Arguments

data
a data table
copy
If the input is a data.table, copy it?

Examples

Run this code
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)
}

Run the code above in your browser using DataCamp Workspace