การเชื่อมต่อ expression ของ data.table

การจัดการข้อมูลด้วย data.table ใน R

Matt Dowle, Arun Srinivasan

Instructors, DataCamp

การเชื่อมต่อ expression

สามารถเชื่อมต่อ expression ของ data.table ได้ เช่น x[...][...][...]

step_1 <- batrips[duration > 3600]
step_2 <- step_1[duration > 3600][order(duration)]
step_2[1:3]
# Same as
batrips[duration > 3600]

batrips[duration > 3600][order(duration)]
batrips[duration > 3600][order(duration)][1:3]
trip_id duration          
 295912 3601     
 347471 3602     
 536050 3602
การจัดการข้อมูลด้วย data.table ใน R

การเชื่อมต่อ expression

# Three start stations with the lowest mean duration
step_1 <- batrips[, .(mn_dur = mean(duration)), by = "start_station"]
step_2 <- step_1[order(mn_dur)]
step_2[1:3]
# Three start stations with the lowest mean duration
batrips[, .(mn_dur = mean(duration)),  
        by = "start_station"][order(mn_dur)][1:3]
                                 start_station   mn_dur
                                 2nd at Folsom 551.0807
 Temporary Transbay Terminal (Howard at Beale) 655.8563
                             2nd at South Park 697.7034
การจัดการข้อมูลด้วย data.table ใน R

uniqueN()

  • uniqueN() คือฟังก์ชันช่วยที่คืนค่าจำนวนเต็มแทนจำนวนค่าที่ไม่ซ้ำกันในอ็อบเจกต์ที่รับเข้า
  • รับได้ทั้ง vector, data.frames และ data.tables
id <- c(1, 2, 2, 1)
uniqueN(id)
2
x <- data.table(id, val = 1:4)
id val
 1   1
 2   2
 2   3
 1   4
uniqueN(x)
4
uniqueN(x, by = "id")
2
การจัดการข้อมูลด้วย data.table ใน R

uniqueN() ร่วมกับ by

คำนวณจำนวน bike id ที่ไม่ซ้ำกันทั้งหมดในแต่ละเดือน

ans <- batrips[, uniqueN(bike_id), by = month(start_date)]
head(ans, 3)
 month    V1   ## <~~ auto naming of cols
     1   605
     2   608
     3   631
การจัดการข้อมูลด้วย data.table ใน R

มาฝึกกันเถอะ!

การจัดการข้อมูลด้วย data.table ใน R

Preparing Video For Download...