Obliczenia według grup

Manipulacja danymi z data.table w R

Matt Dowle, Arun Srinivasan

Instructors, DataCamp

Argument by

Argument by umożliwia obliczenia dla każdej unikalnej wartości kolumn grupujących określonych w by

# How many trips happened from each start_station?
ans <- batrips[, .N, by = "start_station"]
head(ans, 3)
          start_station        N
San Francisco City Hall     2145
 Embarcadero at Sansome    12879
      Steuart at Market    11579
Manipulacja danymi z data.table w R

Argument by

Argument by przyjmuje zarówno wektor character nazw kolumn, jak i list zmiennych/wyrażeń

# Same as batrips[, .N, by = "start_station"]
ans <- batrips[, .N, by = .(start_station)]
head(ans, 3)
          start_station        N
San Francisco City Hall     2145
 Embarcadero at Sansome    12879
      Steuart at Market    11579
Manipulacja danymi z data.table w R

Argument by

Umożliwia zmianę nazw kolumn grupujących w locie

ans <- batrips[, .(no_trips = .N), by = .(start = start_station)]
head(ans, 3)
                  start   no_trips
San Francisco City Hall       2145
 Embarcadero at Sansome      12879
      Steuart at Market      11579
Manipulacja danymi z data.table w R

Wyrażenia w by

Wyrażenie list() lub .() w by umożliwia obliczanie zmiennych grupujących w locie

# Get number of trips for each start_station for each month
ans <- batrips[ , .N, by = .(start_station, mon = month(start_date))]
head(ans, 3)
          start_station mon    N
San Francisco City Hall   1  193
 Embarcadero at Sansome   1  985
      Steuart at Market   1  813
Manipulacja danymi z data.table w R

Czas na ćwiczenia!

Manipulacja danymi z data.table w R

Preparing Video For Download...