R में Scalable Data Processing
Michael Kane
Assistant Professor, Yale University
split()Map()Reduce()split() फंक्शन डेटा को विभाजित करता है
data.frame है जिसे split करना हैfactor या integer है जिसकी वैल्यूज़ partitions तय करती हैं# 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
Map() फंक्शन partitions को प्रोसेस करता है
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 # ...
Reduce() फंक्शन सभी partitions के नतीजे को जोड़ता है
# कॉलम-वार कुल 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