Wybieranie kolumn z data.table

Manipulacja danymi z data.table w R

Matt Dowle, Arun Srinivasan

Instructors, DataCamp

Ogólna składnia data.table (podsumowanie)

Drugi argument j służy do wybierania (i obliczania na) kolumnach

# General form of data.table syntax
DT[i, j, by]
   |  |  |
   |  |  --> grouped by what?
   |  -----> what to do?
   --------> on which rows?
Manipulacja danymi z data.table w R

Wybieranie kolumn za pomocą nazw

Argument j przyjmuje wektor znakowy nazw kolumn

ans <- batrips[, c("trip_id", "duration")]
head(ans, 2)
trip_id duration
139545      435
139546      432
Manipulacja danymi z data.table w R

Wybieranie kolumn za pomocą nazw

batrips_df <- as.data.frame(batrips)
ans <- batrips_df[, "trip_id"]
head(ans, 2)
# The result is a vector, not a data.frame
139545, 139546
ans <- batrips[, "trip_id"]
# Still a data.table, not a vector
head(ans, 2) 
trip_id
139545
139546
Manipulacja danymi z data.table w R

Numery kolumn zamiast nazw również działają poprawnie

ans <- batrips[, c(2, 4)]
head(ans, 2)
duration  start_station
435       San Francisco City Hall
432       San Francisco City Hall

Jednakże uznajemy to za złą praktykę

# If the order of columns changes, the result is wrong
batrips[, c(2, 4)]

# The result is always correct, no matter the order
batrips[, c("duration", "start_station")]
Manipulacja danymi z data.table w R

Odznaczanie kolumn wektorem znakowym

  • -c("col1", "col2", ...) odznacza podane kolumny
  • Funkcja dostępna tylko w data.table
  • Użycie ! zamiast - działa tak samo
# Select all cols *except* those shown below
ans <- batrips[, -c("start_date", "end_date", "end_station")]
head(ans, 1)
trip_id  duration  start_station             start_terminal  bike_id  end_terminal
139545   435       San Francisco City Hall   58              65       473 

subscription_type  zip_code
Subscriber         94612
Manipulacja danymi z data.table w R

Wybieranie kolumn w stylu data.table

Pamiętasz, jak kolumny były używane jak zmienne w argumencie i w poprzednim rozdziale?

# Recap the "i" argument
# All trips more than an hour
batrips[duration > 3600]

Podobnie, można użyć listy zmiennych (nazw kolumn) do wyboru kolumn

ans <- batrips[, list(trip_id, dur = duration)]
head(ans, 2)
trip_id     dur
139545      435
139546      432
Manipulacja danymi z data.table w R

Gdy wybierana jest jedna kolumna bez opakowania zmiennej w list(), zwracany jest vector

# Select a single column and return a data.table
ans <- batrips[, list(trip_id)]
head(ans ,2)
trip_id
139545
139546
# Select a single column and return a vector
ans <- batrips[, trip_id]
head(ans, 2)
139545 139546
Manipulacja danymi z data.table w R

Wybieranie kolumn w stylu data.table

.() jest aliasem list() dla wygody

# .() is the same as list()
ans <- batrips[, .(trip_id, duration)]
head(ans, 2)
trip_id duration
139545      435
139546      432
Manipulacja danymi z data.table w R

Czas na ćwiczenia!

Manipulacja danymi z data.table w R

Preparing Video For Download...