การจัดการข้อมูลด้วย data.table ใน R
Matt Dowle, Arun Srinivasan
Instructors, DataCamp
อาร์กิวเมนต์ by ช่วยให้คำนวณตามแต่ละค่าที่ไม่ซ้ำกันของคอลัมน์ที่ระบุใน 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
อาร์กิวเมนต์ by รับได้ทั้ง character vector ของชื่อคอลัมน์ และ list ของตัวแปร/นิพจน์
# 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
สามารถเปลี่ยนชื่อคอลัมน์จัดกลุ่มได้ทันที
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
นิพจน์ list() หรือ .() ใน by ช่วยให้คำนวณตัวแปรจัดกลุ่มได้ทันที
# 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
การจัดการข้อมูลด้วย data.table ใน R