Calcule pe coloane în stilul data.table

Manipularea datelor cu data.table în R

Matt Dowle, Arun Srinivasan

Instructors, DataCamp

Calcule pe coloane

Deoarece coloanele pot fi referite ca variabile, puteți calcula direct pe ele în j

# Compute mean of duration column using the data.table way
ans <- batrips[, mean(duration)]
1131.967
# Compute mean of duration column using the data.frame way
ans <- mean(batrips[, "duration"])
1131.967
Manipularea datelor cu data.table în R

Calcule pe rânduri și coloane

Combinarea i și j este simplă

# Compute mean of duration column for "Japantown" start station
batrips[start_station == "Japantown", mean(duration)]
2464.331
Manipularea datelor cu data.table în R

Simbolul special .N în j

  • .N poate fi utilizat și în j
  • Util în special pentru a obține numărul de rânduri după filtrarea în i
# How many trips started from "Japantown"?
batrips[start_station == "Japantown", .N]
902
# Compare this to the data.frame way
nrow(batrips[batrips$start_station == "Japantown", ])
902
Manipularea datelor cu data.table în R

Să exersăm!

Manipularea datelor cu data.table în R

Preparing Video For Download...