Split-Apply-Combine

R में Scalable Data Processing

Michael Kane

Assistant Professor, Yale University

Split-Apply-Combine

  • Split: split()
  • Apply: Map()
  • Combine: Reduce()
R में Scalable Data Processing

split() से partition करें

split() फंक्शन डेटा को विभाजित करता है

  • पहला आर्ग्युमेंट वह vector या data.frame है जिसे split करना है
  • दूसरा आर्ग्युमेंट वह factor या integer है जिसकी वैल्यूज़ partitions तय करती हैं
R में Scalable Data Processing
# mortgage डेटा में हर साल के अनुरूप पंक्तियाँ लें
 year_splits <- split(1:nrow(mort), mort[,"year"])
# year_splits एक list है
class(year_splits)
"list"
# जिन सालों पर हमने split किया है
names(year_splits)
"2008" "2009" "2010" "2011" "2012" "2013" "2014" "2015"
# साल 2010 के पहले कुछ row इंडेक्स
year_splits[["2010"]][1:10]
1  6  7 10 21 23 24 27 29 38
R में Scalable Data Processing

Map() से compute करें

Map() फंक्शन partitions को प्रोसेस करता है

  • पहला आर्ग्युमेंट वह फंक्शन है जो हर partition पर लगेगा
  • दूसरा आर्ग्युमेंट partitions हैं
R में Scalable Data Processing

Map() से compute करें

col_missing_count <- function(mort) {
   apply(mort, 2, function(x) sum(x == 9))} 
# हर साल के लिए सभी कॉलम में missing वैल्यू की गिनती करें
missing_by_year <- Map(
   function(x) col_missing_count(mort[x, ]),
   year_splits)

missing_by_year[["2008"]]

enterprise record_number msa 0 12 0 # ...
R में Scalable Data Processing

Reduce() से combine करें

Reduce() फंक्शन सभी partitions के नतीजे को जोड़ता है

  • पहला आर्ग्युमेंट वह फंक्शन है जिससे combine करना है
  • दूसरा आर्ग्युमेंट partitioned डेटा है
R में Scalable Data Processing
# कॉलम-वार कुल missing वैल्यू निकालें
Reduce(`+`, missing_by_year)
enterprise         record_number                   msa 
         0                    64                     0 
# ... 
# rownames को वर्ष से लेबल करें
mby <- Reduce(rbind, missing_by_year)
row.names(mby) <- names(year_splits)
mby[1:3, 1:3]
     enterprise record_number msa
2008          0            12   0
2009          0             8   0
2010          0            10   0

R में Scalable Data Processing

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

R में Scalable Data Processing

Preparing Video For Download...