data.table में पंक्तियाँ फ़िल्टर करना

R में data.table के साथ Data Manipulation

Matt Dowle and Arun Srinivasan

Instructors, DataCamp

data.table सिंटैक्स का सामान्य रूप

पहला आर्गुमेंट i पंक्तियों को subset या filter करने के लिए होता है

# data.table सिंटैक्स का सामान्य रूप
DT[i, j, by]
   |  |  |
   |  |  --> किस पर group करें?
   |  -----> क्या करना है?
   --------> किन पंक्तियों पर?
R में data.table के साथ Data Manipulation

Row numbers

# batrips की 3rd और 4th पंक्तियाँ लें
batrips[3:4]

# यही
batrips[3:4, ]
# पहली पाँच पंक्तियों को छोड़कर बाकी लें
batrips[-(1:5)] 

# यही
batrips[!(1:5)]
R में data.table के साथ Data Manipulation

Special symbol .N

  • .N एक integer मान है जिसमें data.table की पंक्तियों की संख्या होती है
  • i में nrow(x) का उपयोगी विकल्प
nrow(batrips) 
326339
batrips[326339]
trip_id duration
588914      364
# आख़िरी पंक्ति लौटाता है
batrips[.N] 
trip_id duration
588914      364
# आख़िरी 10 पंक्तियों को छोड़कर बाकी लौटाएँ
ans <- batrips[1:(.N-10)] 
nrow(ans)
326329
R में data.table के साथ Data Manipulation

Logical expressions (I)

# वे पंक्तियाँ लें जहाँ subscription_type "Subscriber" हो
batrips[subscription_type == "Subscriber"]

# अगर batrips केवल data frame होता
batrips[batrips$subscription_type == "Subscriber", ]
R में data.table के साथ Data Manipulation

Logical expressions (II)

# वे पंक्तियाँ लें जहाँ start_terminal = 58 और end_terminal 65 नहीं है
batrips[start_terminal == 58 & end_terminal != 65]

# अगर batrips केवल data frame होता
batrips[batrips$start_terminal == 58 & batrips$end_terminal != 65]
R में data.table के साथ Data Manipulation

Logical expressions (III)

स्पीड के लिए सेकेंडरी इंडाइसेज़ से स्वतः optimized

set.seed(1)
dt <- data.table(x = sample(10000, 10e6, TRUE), 
                 y = sample(letters, 1e6, TRUE))
indices(dt)
NULL
# पहली बार 0.207s 
#(इंडेक्स बनाना + subset)
system.time(dt[x == 900])
user  system elapsed 
0.207   0.015   0.226 
indices(dt)
"x"
# बाद के रन पर 0.002s
#(इंडेक्स से instant subset)
system.time(dt[x == 900])
user  system elapsed 
0.002   0.000   0.002
R में data.table के साथ Data Manipulation

अभ्यास करते हैं!

R में data.table के साथ Data Manipulation

Preparing Video For Download...