data.tableによる列の計算

R の data.table によるデータ操作

Matt Dowle, Arun Srinivasan

Instructors, DataCamp

列の計算

列は変数として参照できるため、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
R の data.table によるデータ操作

行と列の計算

ij の組み合わせは_簡単_です

# Compute mean of duration column for "Japantown" start station
batrips[start_station == "Japantown", mean(duration)]
2464.331
R の data.table によるデータ操作

jにおける特殊シンボル .N

  • .Nj でも使用できます
  • 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
R の data.table によるデータ操作

さあ、練習しましょう!

R の data.table によるデータ操作

Preparing Video For Download...