r - कैसे डेटा फ्रेम(आंतरिक, बाहरी, बाएं, दाएं) में शामिल(विलय)?
join merge (8)
- Using
merge
function we can select the variable of left table or right table, same way like we all familiar with select statement in SQL (EX : Select a.* ...or Select b.* from .....) We have to add extra code which will subset from the newly joined table .
SQL :-
select a.* from df1 a inner join df2 b on a.CustomerId=b.CustomerId
आर: -
merge(df1, df2, by.x = "CustomerId", by.y = "CustomerId")[,names(df1)]
उसी तरह
एसक्यूएल: -
select b.* from df1 a inner join df2 b on a.CustomerId=b.CustomerId
आर: -
merge(df1, df2, by.x = "CustomerId", by.y = "CustomerId")[,names(df2)]
दो डेटा फ्रेम दिए गए:
df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))
df1
# CustomerId Product
# 1 Toaster
# 2 Toaster
# 3 Toaster
# 4 Radio
# 5 Radio
# 6 Radio
df2
# CustomerId State
# 2 Alabama
# 4 Alabama
# 6 Ohio
मैं डेटाबेस शैली कैसे कर सकता हूं, यानी, एसक्यूएल शैली, जुड़ता है ? यही है, मैं कैसे प्राप्त करूं:
-
df1
औरdf2
2 का एक आंतरिकdf2
:
केवल पंक्तियों को वापस करें जिसमें बाएं तालिका में दाएं तालिका में मिलान कुंजी है। -
df1
औरdf2
2 का बाहरीdf2
:
दोनों तालिकाओं से सभी पंक्तियों को वापस लाता है, बाएं से रिकॉर्ड्स में शामिल होता है जिसमें दाएं तालिका में मिलान कुंजी होती है। -
df1
औरdf2
बाएं बाहरी जुड़ें (या बस शामिल हों)
बाएं टेबल से सभी पंक्तियां, और दाएं तालिका से मिलान करने वाली कुंजियों वाली किसी भी पंक्ति को वापस करें। -
df1
औरdf2
का सही बाहरीdf2
दाएं टेबल से सभी पंक्तियां, और बाएं तालिका से मिलान करने वाली कुंजियों वाली किसी भी पंक्ति को वापस करें।
अतिरिक्त श्रेय:
मैं एक एसक्यूएल शैली का चयन कथन कैसे कर सकता हूं?
2014 में नया:
विशेष रूप से यदि आप सामान्य रूप से डेटा मैनिपुलेशन (सॉर्टिंग, फ़िल्टरिंग, सबसेटिंग, संक्षेप आदि सहित) में रुचि रखते हैं, तो आपको निश्चित रूप से dplyr
पर एक नज़र dplyr
, जो आपके काम को विशेष रूप से डेटा के साथ सुविधाजनक बनाने के लिए डिज़ाइन किए गए विभिन्न कार्यों के साथ आता है फ्रेम और कुछ अन्य डेटाबेस प्रकार। यह काफी विस्तृत एसक्यूएल इंटरफेस भी प्रदान करता है, और यहां तक कि एक समारोह भी (अधिकांश) एसक्यूएल कोड को सीधे आर में परिवर्तित करने के लिए करता है।
Dplyr पैकेज में चार जुड़ने-संबंधित कार्यों (उद्धरण के लिए) हैं:
-
inner_join(x, y, by = NULL, copy = FALSE, ...)
: x से सभी पंक्तियों को वापस करें जहां y में मिलान मान हैं, और x और y के सभी कॉलम -
left_join(x, y, by = NULL, copy = FALSE, ...)
: x से सभी पंक्तियों को वापस करें, और x और y से सभी कॉलम -
semi_join(x, y, by = NULL, copy = FALSE, ...)
: x से सभी पंक्तियों को वापस करें जहां y में मिलान मान हैं, केवल x से कॉलम रखते हुए। -
anti_join(x, y, by = NULL, copy = FALSE, ...)
: x से सभी पंक्तियों को वापस करें जहां y में मिलान मान नहीं हैं, केवल x से कॉलम रखते हुए
यह सब here बहुत विस्तार से है।
कॉलम का चयन select(df,"column")
द्वारा किया जा सकता है। यदि यह आपके लिए एसक्यूएल-आईएसएच पर्याप्त नहीं है, तो sql()
फ़ंक्शन है, जिसमें आप एसक्यूएल कोड दर्ज कर सकते हैं, और यह आपके द्वारा निर्दिष्ट ऑपरेशन करेगा जैसा कि आप आर में लिख रहे थे (अधिक जानकारी के लिए , कृपया dplyr / डेटाबेस vignette देखें )। उदाहरण के लिए, यदि सही ढंग से लागू किया गया है, तो sql("SELECT * FROM hflights")
"hflights" dplyr तालिका (एक "tbl") से सभी कॉलम का चयन करेगा।
~ 1 मिलियन पंक्तियों के साथ दो डेटा फ्रेम में शामिल होने में, 2 कॉलम वाले एक और दूसरा ~ 20 के साथ, मुझे आश्चर्यजनक रूप से merge(..., all.x = TRUE, all.y = TRUE)
मिला है merge(..., all.x = TRUE, all.y = TRUE)
तेज़ होने के बाद dplyr::full_join()
। यह dplyr v0.4 के साथ है
विलय ~ 17 सेकंड लेता है, full_join ~ 65 सेकंड लेता है।
हालांकि कुछ भोजन, क्योंकि मैं आमतौर पर हेरफेर कार्यों के लिए dplyr करने के लिए डिफ़ॉल्ट।
आंतरिक जुड़ने के लिए डेटाटेबल दृष्टिकोण है, जो बहुत समय और मेमोरी कुशल है (और कुछ बड़े डेटा.फ्रेम के लिए आवश्यक है):
library(data.table)
dt1 <- data.table(df1, key = "CustomerId")
dt2 <- data.table(df2, key = "CustomerId")
joined.dt1.dt.2 <- dt1[dt2]
merge
डेटा.tables पर भी काम करता है (क्योंकि यह सामान्य है और merge.data.table
कॉल merge.data.table
)
merge(dt1, dt2)
पर दस्तावेज डेटाटेबल:
डेटाटेबल मर्ज ऑपरेशन कैसे करें
एसक्यूएल का अनुवाद आर डेटाटेबल वाक्यविन्यास में विदेशी कुंजी पर जुड़ता है
बड़े डेटा के लिए विलय करने के लिए कुशल विकल्प। फ्रेम आर
आर में डेटाटेबल के साथ मूल बाएं बाहरी कैसे जुड़ें?
फिर भी एक और विकल्प plyr पैकेज में पाया गया plyr है
library(plyr)
join(df1, df2,
type = "inner")
# CustomerId Product State
# 1 2 Toaster Alabama
# 2 4 Radio Alabama
# 3 6 Radio Ohio
type
लिए विकल्प: inner
, left
, right
, full
।
से ?join
: merge
विपरीत, [ join
] एक्स के आदेश को सुरक्षित रखता है इससे कोई फर्क नहीं पड़ता कि किस प्रकार का उपयोग किया जाता है।
डेटासेट में शामिल होने के लिए डेटा.table विधियों पर अपडेट करें। प्रत्येक प्रकार के शामिल होने के लिए उदाहरण नीचे देखें। दो विधियां हैं, एक [.data.table
से दूसरे डेटाटेबल को सब्सट्रेट करने के पहले तर्क के रूप में पास करते समय, एक और तरीका merge
फ़ंक्शन का उपयोग करना है जो तेजी से डेटा.table विधि को भेजा जाता है।
2016-04-01 को अपडेट करें - और यह अप्रैल फूल मजाक नहीं है!
डेटाटेबल जॉइन के 1.9.7 संस्करण में अब मौजूदा इंडेक्स का उपयोग करने में सक्षम हैं जो शामिल होने के समय को काफी कम करता है। कोड और बेंचमार्क के नीचे शामिल होने पर डेटाटेबल इंडेक्स का उपयोग नहीं करता है । यदि आप वास्तविक समय में शामिल होने की तलाश में हैं तो आपको डेटाटेबल इंडेक्स का उपयोग करना चाहिए।
df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
df2 = data.frame(CustomerId = c(2L, 4L, 7L), State = c(rep("Alabama", 2), rep("Ohio", 1))) # one value changed to show full outer join
library(data.table)
dt1 = as.data.table(df1)
dt2 = as.data.table(df2)
setkey(dt1, CustomerId)
setkey(dt2, CustomerId)
# right outer join keyed data.tables
dt1[dt2]
setkey(dt1, NULL)
setkey(dt2, NULL)
# right outer join unkeyed data.tables - use `on` argument
dt1[dt2, on = "CustomerId"]
# left outer join - swap dt1 with dt2
dt2[dt1, on = "CustomerId"]
# inner join - use `nomatch` argument
dt1[dt2, nomatch=0L, on = "CustomerId"]
# anti join - use `!` operator
dt1[!dt2, on = "CustomerId"]
# inner join
merge(dt1, dt2, by = "CustomerId")
# full outer join
merge(dt1, dt2, by = "CustomerId", all = TRUE)
# see ?merge.data.table arguments for other cases
बेंचमार्क परीक्षण बेस आर, sqldf, dplyr और data.table के नीचे।
बेंचमार्क unkeyed / unindexed डेटासेट परीक्षण करता है। यदि आप अपने डेटाटेबल्स या sqldf के साथ इंडेक्स पर चाबियाँ का उपयोग कर रहे हैं तो आप भी बेहतर प्रदर्शन प्राप्त कर सकते हैं। बेस आर और डीप्लर में इंडेक्स या चाबियाँ नहीं हैं इसलिए मैंने उस परिदृश्य को बेंचमार्क में शामिल नहीं किया।
बेंचमार्क 5 एम -1 पंक्ति पंक्तियों पर किया जाता है, कॉलम में 5 एम -2 सामान्य मान होते हैं, इसलिए प्रत्येक परिदृश्य (बाएं, दाएं, पूर्ण, भीतरी) का परीक्षण किया जा सकता है और इसमें शामिल होने के लिए अभी भी मामूली नहीं है।
library(microbenchmark)
library(sqldf)
library(dplyr)
library(data.table)
n = 5e6
set.seed(123)
df1 = data.frame(x=sample(n,n-1L), y1=rnorm(n-1L))
df2 = data.frame(x=sample(n,n-1L), y2=rnorm(n-1L))
dt1 = as.data.table(df1)
dt2 = as.data.table(df2)
# inner join
microbenchmark(times = 10L,
base = merge(df1, df2, by = "x"),
sqldf = sqldf("SELECT * FROM df1 INNER JOIN df2 ON df1.x = df2.x"),
dplyr = inner_join(df1, df2, by = "x"),
data.table = dt1[dt2, nomatch = 0L, on = "x"])
#Unit: milliseconds
# expr min lq mean median uq max neval
# base 15546.0097 16083.4915 16687.117 16539.0148 17388.290 18513.216 10
# sqldf 44392.6685 44709.7128 45096.401 45067.7461 45504.376 45563.472 10
# dplyr 4124.0068 4248.7758 4281.122 4272.3619 4342.829 4411.388 10
# data.table 937.2461 946.0227 1053.411 973.0805 1214.300 1281.958 10
# left outer join
microbenchmark(times = 10L,
base = merge(df1, df2, by = "x", all.x = TRUE),
sqldf = sqldf("SELECT * FROM df1 LEFT OUTER JOIN df2 ON df1.x = df2.x"),
dplyr = left_join(df1, df2, by = c("x"="x")),
data.table = dt2[dt1, on = "x"])
#Unit: milliseconds
# expr min lq mean median uq max neval
# base 16140.791 17107.7366 17441.9538 17414.6263 17821.9035 19453.034 10
# sqldf 43656.633 44141.9186 44777.1872 44498.7191 45288.7406 47108.900 10
# dplyr 4062.153 4352.8021 4780.3221 4409.1186 4450.9301 8385.050 10
# data.table 823.218 823.5557 901.0383 837.9206 883.3292 1277.239 10
# right outer join
microbenchmark(times = 10L,
base = merge(df1, df2, by = "x", all.y = TRUE),
sqldf = sqldf("SELECT * FROM df2 LEFT OUTER JOIN df1 ON df2.x = df1.x"),
dplyr = right_join(df1, df2, by = "x"),
data.table = dt1[dt2, on = "x"])
#Unit: milliseconds
# expr min lq mean median uq max neval
# base 15821.3351 15954.9927 16347.3093 16044.3500 16621.887 17604.794 10
# sqldf 43635.5308 43761.3532 43984.3682 43969.0081 44044.461 44499.891 10
# dplyr 3936.0329 4028.1239 4102.4167 4045.0854 4219.958 4307.350 10
# data.table 820.8535 835.9101 918.5243 887.0207 1005.721 1068.919 10
# full outer join
microbenchmark(times = 10L,
base = merge(df1, df2, by = "x", all = TRUE),
#sqldf = sqldf("SELECT * FROM df1 FULL OUTER JOIN df2 ON df1.x = df2.x"), # not supported
dplyr = full_join(df1, df2, by = "x"),
data.table = merge(dt1, dt2, by = "x", all = TRUE))
#Unit: seconds
# expr min lq mean median uq max neval
# base 16.176423 16.908908 17.485457 17.364857 18.271790 18.626762 10
# dplyr 7.610498 7.666426 7.745850 7.710638 7.832125 7.951426 10
# data.table 2.052590 2.130317 2.352626 2.208913 2.470721 2.951948 10
मैं गैबर ग्रोथेंडिक के sqldf पैकेज को देखने की अनुशंसा करता हूं , जो आपको इन परिचालनों को एसक्यूएल में व्यक्त करने की अनुमति देता है।
library(sqldf)
## inner join
df3 <- sqldf("SELECT CustomerId, Product, State
FROM df1
JOIN df2 USING(CustomerID)")
## left join (substitute 'right' for right join)
df4 <- sqldf("SELECT CustomerId, Product, State
FROM df1
LEFT JOIN df2 USING(CustomerID)")
मुझे एसक्यूएल सिंटैक्स अपने आर समकक्ष से सरल और अधिक प्राकृतिक होने लगता है (लेकिन यह सिर्फ मेरे आरडीबीएमएस पूर्वाग्रह को प्रतिबिंबित कर सकता है)।
जुड़ने के बारे में अधिक जानकारी के लिए गैबर के एसक्ल्डफ गिटहब देखें।
0..*:0..1
साथ बाएं जुड़ने के मामले में 0..*:0..1
कार्डिनालिटी या दाएं 0..1:0..*
साथ जुड़ें 0..1:0..*
कार्डिनलिटी में शामिल होने के लिए एकतरफा कॉलम को शामिल करना संभव है ( 0..1
तालिका) सीधे जोनी ( 0..*
तालिका) पर, और इस तरह डेटा की पूरी तरह से नई तालिका के निर्माण से बचें। इसके लिए जॉइन से मुख्य कॉलम को जॉइनर और इंडेक्सिंग में जोड़ना आवश्यक है + असाइनमेंट के अनुसार जॉइनर की पंक्तियों को ऑर्डर करना।
यदि कुंजी एक कॉलम है, तो हम match()
लिए match()
करने के लिए एक कॉल का उपयोग कर सकते हैं। यही वह मामला है जिसे मैं इस जवाब में शामिल करूंगा।
यहां ओपी के आधार पर एक उदाहरण दिया गया है, सिवाय इसके कि मैंने df2
गैर-मेलिंग कुंजी के मामले की जांच करने के लिए 7 की आईडी के साथ df2
अतिरिक्त पंक्ति जोड़ दी है। यह प्रभावी रूप से df1
शामिल हो df2
:
df1 <- data.frame(CustomerId=1:6,Product=c(rep('Toaster',3L),rep('Radio',3L)));
df2 <- data.frame(CustomerId=c(2L,4L,6L,7L),State=c(rep('Alabama',2L),'Ohio','Texas'));
df1[names(df2)[-1L]] <- df2[match(df1[,1L],df2[,1L]),-1L];
df1;
## CustomerId Product State
## 1 1 Toaster <NA>
## 2 2 Toaster Alabama
## 3 3 Toaster <NA>
## 4 4 Radio Alabama
## 5 5 Radio <NA>
## 6 6 Radio Ohio
उपर्युक्त में मैंने एक धारणा को कड़ी-कोडित किया कि कुंजी कॉलम इनपुट तालिका दोनों का पहला स्तंभ है। मैं तर्क दूंगा कि, सामान्य रूप से, यह एक अनुचित धारणा नहीं है, क्योंकि, यदि आपके पास एक प्रमुख कॉलम वाला डेटा है। तो यह अजीब होगा अगर इसे डेटा के पहले कॉलम के रूप में सेट नहीं किया गया था। आरंभ। और आप इसे बनाने के लिए कॉलम को हमेशा पुन: व्यवस्थित कर सकते हैं। इस धारणा का एक फायदेमंद परिणाम यह है कि कुंजी कॉलम का नाम हार्ड-कोड नहीं होना चाहिए, हालांकि मुझे लगता है कि यह सिर्फ एक धारणा को दूसरे के साथ बदल रहा है। चिंता पूर्णांक अनुक्रमण, साथ ही गति का एक और लाभ है। नीचे दिए गए बेंचमार्क में मैं प्रतिस्पर्धात्मक कार्यान्वयन से मेल खाने के लिए स्ट्रिंग नाम अनुक्रमण का उपयोग करने के लिए कार्यान्वयन को बदल दूंगा।
मुझे लगता है कि यह एक विशेष रूप से उचित समाधान है यदि आपके पास कई टेबल हैं जिन्हें आप एक बड़ी तालिका के खिलाफ शामिल करना चाहते हैं। प्रत्येक विलय के लिए पूरी तालिका को बार-बार पुनर्निर्माण करना अनावश्यक और अक्षम होगा।
दूसरी तरफ, यदि आपको किसी भी कारण से इस ऑपरेशन के माध्यम से अनियंत्रित रहने के लिए जॉनी की आवश्यकता है, तो इस समाधान का उपयोग नहीं किया जा सकता है, क्योंकि यह सीधे जॉनी को संशोधित करता है। यद्यपि उस स्थिति में आप प्रतिलिपि बना सकते हैं और कॉपी पर इन-प्लेस असाइनमेंट कर सकते हैं।
एक साइड नोट के रूप में, मैंने संक्षेप में बहुआयामी कुंजी के लिए संभावित मिलान समाधानों को देखा। दुर्भाग्यवश, मुझे मिले एकमात्र मिलान समाधान थे:
- अक्षम concatenations। उदाहरण के लिए
match(interaction(df1$a,df1$b),interaction(df2$a,df2$b))
, याpaste()
साथ एक ही विचार। - अक्षम कार्टेसियन संयोजन, उदाहरण के लिए
outer(df1$a,df2$a,`==`) & outer(df1$b,df2$b,`==`)
। - बेस आर
merge()
और समकक्ष पैकेज-आधारित विलय फ़ंक्शंस, जो हमेशा मर्ज किए गए परिणाम को वापस करने के लिए एक नई तालिका आवंटित करते हैं, और इस तरह इन-प्लेस असाइनमेंट-आधारित समाधान के लिए उपयुक्त नहीं हैं।
उदाहरण के लिए, विभिन्न डेटा फ्रेमों पर एकाधिक कॉलम मिलान करना और परिणाम के रूप में अन्य कॉलम प्राप्त करना , दो कॉलम से दो कॉलम मिलान करना, एकाधिक कॉलम पर मिलान करना , और इस प्रश्न का डुप्लिकेट जहां मैं मूल रूप से इन-प्लेस समाधान के साथ आया था, संयोजन आर में विभिन्न पंक्तियों के साथ दो डेटा फ्रेम ।
बेंचमार्किंग
मैंने यह देखने के लिए अपना स्वयं का बेंचमार्किंग करने का फैसला किया कि इन-प्लेस असाइनमेंट दृष्टिकोण इस प्रश्न में दिए गए अन्य समाधानों की तुलना कैसे करता है।
परीक्षण कोड:
library(microbenchmark);
library(data.table);
library(sqldf);
library(plyr);
library(dplyr);
solSpecs <- list(
merge=list(testFuncs=list(
inner=function(df1,df2,key) merge(df1,df2,key),
left =function(df1,df2,key) merge(df1,df2,key,all.x=T),
right=function(df1,df2,key) merge(df1,df2,key,all.y=T),
full =function(df1,df2,key) merge(df1,df2,key,all=T)
)),
data.table.unkeyed=list(argSpec='data.table.unkeyed',testFuncs=list(
inner=function(dt1,dt2,key) dt1[dt2,on=key,nomatch=0L,allow.cartesian=T],
left =function(dt1,dt2,key) dt2[dt1,on=key,allow.cartesian=T],
right=function(dt1,dt2,key) dt1[dt2,on=key,allow.cartesian=T],
full =function(dt1,dt2,key) merge(dt1,dt2,key,all=T,allow.cartesian=T) ## calls merge.data.table()
)),
data.table.keyed=list(argSpec='data.table.keyed',testFuncs=list(
inner=function(dt1,dt2) dt1[dt2,nomatch=0L,allow.cartesian=T],
left =function(dt1,dt2) dt2[dt1,allow.cartesian=T],
right=function(dt1,dt2) dt1[dt2,allow.cartesian=T],
full =function(dt1,dt2) merge(dt1,dt2,all=T,allow.cartesian=T) ## calls merge.data.table()
)),
sqldf.unindexed=list(testFuncs=list( ## note: must pass connection=NULL to avoid running against the live DB connection, which would result in collisions with the residual tables from the last query upload
inner=function(df1,df2,key) sqldf(paste0('select * from df1 inner join df2 using(',paste(collapse=',',key),')'),connection=NULL),
left =function(df1,df2,key) sqldf(paste0('select * from df1 left join df2 using(',paste(collapse=',',key),')'),connection=NULL),
right=function(df1,df2,key) sqldf(paste0('select * from df2 left join df1 using(',paste(collapse=',',key),')'),connection=NULL) ## can't do right join proper, not yet supported; inverted left join is equivalent
##full =function(df1,df2,key) sqldf(paste0('select * from df1 full join df2 using(',paste(collapse=',',key),')'),connection=NULL) ## can't do full join proper, not yet supported; possible to hack it with a union of left joins, but too unreasonable to include in testing
)),
sqldf.indexed=list(testFuncs=list( ## important: requires an active DB connection with preindexed main.df1 and main.df2 ready to go; arguments are actually ignored
inner=function(df1,df2,key) sqldf(paste0('select * from main.df1 inner join main.df2 using(',paste(collapse=',',key),')')),
left =function(df1,df2,key) sqldf(paste0('select * from main.df1 left join main.df2 using(',paste(collapse=',',key),')')),
right=function(df1,df2,key) sqldf(paste0('select * from main.df2 left join main.df1 using(',paste(collapse=',',key),')')) ## can't do right join proper, not yet supported; inverted left join is equivalent
##full =function(df1,df2,key) sqldf(paste0('select * from main.df1 full join main.df2 using(',paste(collapse=',',key),')')) ## can't do full join proper, not yet supported; possible to hack it with a union of left joins, but too unreasonable to include in testing
)),
plyr=list(testFuncs=list(
inner=function(df1,df2,key) join(df1,df2,key,'inner'),
left =function(df1,df2,key) join(df1,df2,key,'left'),
right=function(df1,df2,key) join(df1,df2,key,'right'),
full =function(df1,df2,key) join(df1,df2,key,'full')
)),
dplyr=list(testFuncs=list(
inner=function(df1,df2,key) inner_join(df1,df2,key),
left =function(df1,df2,key) left_join(df1,df2,key),
right=function(df1,df2,key) right_join(df1,df2,key),
full =function(df1,df2,key) full_join(df1,df2,key)
)),
in.place=list(testFuncs=list(
left =function(df1,df2,key) { cns <- setdiff(names(df2),key); df1[cns] <- df2[match(df1[,key],df2[,key]),cns]; df1; },
right=function(df1,df2,key) { cns <- setdiff(names(df1),key); df2[cns] <- df1[match(df2[,key],df1[,key]),cns]; df2; }
))
);
getSolTypes <- function() names(solSpecs);
getJoinTypes <- function() unique(unlist(lapply(solSpecs,function(x) names(x$testFuncs))));
getArgSpec <- function(argSpecs,key=NULL) if (is.null(key)) argSpecs$default else argSpecs[[key]];
initSqldf <- function() {
sqldf(); ## creates sqlite connection on first run, cleans up and closes existing connection otherwise
if (exists('sqldfInitFlag',envir=globalenv(),inherits=F) && sqldfInitFlag) { ## false only on first run
sqldf(); ## creates a new connection
} else {
assign('sqldfInitFlag',T,envir=globalenv()); ## set to true for the one and only time
}; ## end if
invisible();
}; ## end initSqldf()
setUpBenchmarkCall <- function(argSpecs,joinType,solTypes=getSolTypes(),env=parent.frame()) {
## builds and returns a list of expressions suitable for passing to the list argument of microbenchmark(), and assigns variables to resolve symbol references in those expressions
callExpressions <- list();
nms <- character();
for (solType in solTypes) {
testFunc <- solSpecs[[solType]]$testFuncs[[joinType]];
if (is.null(testFunc)) next; ## this join type is not defined for this solution type
testFuncName <- paste0('tf.',solType);
assign(testFuncName,testFunc,envir=env);
argSpecKey <- solSpecs[[solType]]$argSpec;
argSpec <- getArgSpec(argSpecs,argSpecKey);
argList <- setNames(nm=names(argSpec$args),vector('list',length(argSpec$args)));
for (i in seq_along(argSpec$args)) {
argName <- paste0('tfa.',argSpecKey,i);
assign(argName,argSpec$args[[i]],envir=env);
argList[[i]] <- if (i%in%argSpec$copySpec) call('copy',as.symbol(argName)) else as.symbol(argName);
}; ## end for
callExpressions[[length(callExpressions)+1L]] <- do.call(call,c(list(testFuncName),argList),quote=T);
nms[length(nms)+1L] <- solType;
}; ## end for
names(callExpressions) <- nms;
callExpressions;
}; ## end setUpBenchmarkCall()
harmonize <- function(res) {
res <- as.data.frame(res); ## coerce to data.frame
for (ci in which(sapply(res,is.factor))) res[[ci]] <- as.character(res[[ci]]); ## coerce factor columns to character
for (ci in which(sapply(res,is.logical))) res[[ci]] <- as.integer(res[[ci]]); ## coerce logical columns to integer (works around sqldf quirk of munging logicals to integers)
##for (ci in which(sapply(res,inherits,'POSIXct'))) res[[ci]] <- as.double(res[[ci]]); ## coerce POSIXct columns to double (works around sqldf quirk of losing POSIXct class) ----- POSIXct doesn't work at all in sqldf.indexed
res <- res[order(names(res))]; ## order columns
res <- res[do.call(order,res),]; ## order rows
res;
}; ## end harmonize()
checkIdentical <- function(argSpecs,solTypes=getSolTypes()) {
for (joinType in getJoinTypes()) {
callExpressions <- setUpBenchmarkCall(argSpecs,joinType,solTypes);
if (length(callExpressions)<2L) next;
ex <- harmonize(eval(callExpressions[[1L]]));
for (i in seq(2L,len=length(callExpressions)-1L)) {
y <- harmonize(eval(callExpressions[[i]]));
if (!isTRUE(all.equal(ex,y,check.attributes=F))) {
ex <<- ex;
y <<- y;
solType <- names(callExpressions)[i];
stop(paste0('non-identical: ',solType,' ',joinType,'.'));
}; ## end if
}; ## end for
}; ## end for
invisible();
}; ## end checkIdentical()
testJoinType <- function(argSpecs,joinType,solTypes=getSolTypes(),metric=NULL,times=100L) {
callExpressions <- setUpBenchmarkCall(argSpecs,joinType,solTypes);
bm <- microbenchmark(list=callExpressions,times=times);
if (is.null(metric)) return(bm);
bm <- summary(bm);
res <- setNames(nm=names(callExpressions),bm[[metric]]);
attr(res,'unit') <- attr(bm,'unit');
res;
}; ## end testJoinType()
testAllJoinTypes <- function(argSpecs,solTypes=getSolTypes(),metric=NULL,times=100L) {
joinTypes <- getJoinTypes();
resList <- setNames(nm=joinTypes,lapply(joinTypes,function(joinType) testJoinType(argSpecs,joinType,solTypes,metric,times)));
if (is.null(metric)) return(resList);
units <- unname(unlist(lapply(resList,attr,'unit')));
res <- do.call(data.frame,c(list(join=joinTypes),setNames(nm=solTypes,rep(list(rep(NA_real_,length(joinTypes))),length(solTypes))),list(unit=units,stringsAsFactors=F)));
for (i in seq_along(resList)) res[i,match(names(resList[[i]]),names(res))] <- resList[[i]];
res;
}; ## end testAllJoinTypes()
testGrid <- function(makeArgSpecsFunc,sizes,overlaps,solTypes=getSolTypes(),joinTypes=getJoinTypes(),metric='median',times=100L) {
res <- expand.grid(size=sizes,overlap=overlaps,joinType=joinTypes,stringsAsFactors=F);
res[solTypes] <- NA_real_;
res$unit <- NA_character_;
for (ri in seq_len(nrow(res))) {
size <- res$size[ri];
overlap <- res$overlap[ri];
joinType <- res$joinType[ri];
argSpecs <- makeArgSpecsFunc(size,overlap);
checkIdentical(argSpecs,solTypes);
cur <- testJoinType(argSpecs,joinType,solTypes,metric,times);
res[ri,match(names(cur),names(res))] <- cur;
res$unit[ri] <- attr(cur,'unit');
}; ## end for
res;
}; ## end testGrid()
यहां ओपी के आधार पर उदाहरण का बेंचमार्क दिया गया है जिसे मैंने पहले दिखाया था:
## OP's example, supplemented with a non-matching row in df2
argSpecs <- list(
default=list(copySpec=1:2,args=list(
df1 <- data.frame(CustomerId=1:6,Product=c(rep('Toaster',3L),rep('Radio',3L))),
df2 <- data.frame(CustomerId=c(2L,4L,6L,7L),State=c(rep('Alabama',2L),'Ohio','Texas')),
'CustomerId'
)),
data.table.unkeyed=list(copySpec=1:2,args=list(
as.data.table(df1),
as.data.table(df2),
'CustomerId'
)),
data.table.keyed=list(copySpec=1:2,args=list(
setkey(as.data.table(df1),CustomerId),
setkey(as.data.table(df2),CustomerId)
))
);
## prepare sqldf
initSqldf();
sqldf('create index df1_key on df1(CustomerId);'); ## upload and create an sqlite index on df1
sqldf('create index df2_key on df2(CustomerId);'); ## upload and create an sqlite index on df2
checkIdentical(argSpecs);
testAllJoinTypes(argSpecs,metric='median');
## join merge data.table.unkeyed data.table.keyed sqldf.unindexed sqldf.indexed plyr dplyr in.place unit
## 1 inner 644.259 861.9345 923.516 9157.752 1580.390 959.2250 270.9190 NA microseconds
## 2 left 713.539 888.0205 910.045 8820.334 1529.714 968.4195 270.9185 224.3045 microseconds
## 3 right 1221.804 909.1900 923.944 8930.668 1533.135 1063.7860 269.8495 218.1035 microseconds
## 4 full 1302.203 3107.5380 3184.729 NA NA 1593.6475 270.7055 NA microseconds
यहां मैं यादृच्छिक इनपुट डेटा पर बेंचमार्क करता हूं, दो इनपुट टेबल के बीच कुंजी ओवरलैप के विभिन्न स्केल और विभिन्न पैटर्न का प्रयास करता हूं। यह बेंचमार्क अभी भी एकल कॉलम पूर्णांक कुंजी के मामले तक ही सीमित है। साथ ही, यह सुनिश्चित करने के लिए कि इन-प्लेस समाधान समान तालिकाओं के बाएं और दाएं दोनों में शामिल होगा, सभी यादृच्छिक परीक्षण डेटा 0..1:0..1
कार्डिनिटी का उपयोग करता है। यह दूसरे डेटा के मुख्य कॉलम उत्पन्न करते समय पहले डेटा के मुख्य कॉलम को प्रतिस्थापित किए बिना नमूनाकरण द्वारा कार्यान्वित किया जाता है। फ्रेम।
makeArgSpecs.singleIntegerKey.optionalOneToOne <- function(size,overlap) {
com <- as.integer(size*overlap);
argSpecs <- list(
default=list(copySpec=1:2,args=list(
df1 <- data.frame(id=sample(size),y1=rnorm(size),y2=rnorm(size)),
df2 <- data.frame(id=sample(c(if (com>0L) sample(df1$id,com) else integer(),seq(size+1L,len=size-com))),y3=rnorm(size),y4=rnorm(size)),
'id'
)),
data.table.unkeyed=list(copySpec=1:2,args=list(
as.data.table(df1),
as.data.table(df2),
'id'
)),
data.table.keyed=list(copySpec=1:2,args=list(
setkey(as.data.table(df1),id),
setkey(as.data.table(df2),id)
))
);
## prepare sqldf
initSqldf();
sqldf('create index df1_key on df1(id);'); ## upload and create an sqlite index on df1
sqldf('create index df2_key on df2(id);'); ## upload and create an sqlite index on df2
argSpecs;
}; ## end makeArgSpecs.singleIntegerKey.optionalOneToOne()
## cross of various input sizes and key overlaps
sizes <- c(1e1L,1e3L,1e6L);
overlaps <- c(0.99,0.5,0.01);
system.time({ res <- testGrid(makeArgSpecs.singleIntegerKey.optionalOneToOne,sizes,overlaps); });
## user system elapsed
## 22024.65 12308.63 34493.19
I wrote some code to create log-log plots of the above results. I generated a separate plot for each overlap percentage. It's a little bit cluttered, but I like having all the solution types and join types represented in the same plot.
I used spline interpolation to show a smooth curve for each solution/join type combination, drawn with individual pch symbols. The join type is captured by the pch symbol, using a dot for inner, left and right angle brackets for left and right, and a diamond for full. The solution type is captured by the color as shown in the legend.
plotRes <- function(res,titleFunc,useFloor=F) {
solTypes <- setdiff(names(res),c('size','overlap','joinType','unit')); ## derive from res
normMult <- c(microseconds=1e-3,milliseconds=1); ## normalize to milliseconds
joinTypes <- getJoinTypes();
cols <- c(merge='purple',data.table.unkeyed='blue',data.table.keyed='#00DDDD',sqldf.unindexed='brown',sqldf.indexed='orange',plyr='red',dplyr='#00BB00',in.place='magenta');
pchs <- list(inner=20L,left='<',right='>',full=23L);
cexs <- c(inner=0.7,left=1,right=1,full=0.7);
NP <- 60L;
ord <- order(decreasing=T,colMeans(res[res$size==max(res$size),solTypes],na.rm=T));
ymajors <- data.frame(y=c(1,1e3),label=c('1ms','1s'),stringsAsFactors=F);
for (overlap in unique(res$overlap)) {
x1 <- res[res$overlap==overlap,];
x1[solTypes] <- x1[solTypes]*normMult[x1$unit]; x1$unit <- NULL;
xlim <- c(1e1,max(x1$size));
xticks <- 10^seq(log10(xlim[1L]),log10(xlim[2L]));
ylim <- c(1e-1,10^((if (useFloor) floor else ceiling)(log10(max(x1[solTypes],na.rm=T))))); ## use floor() to zoom in a little more, only sqldf.unindexed will break above, but xpd=NA will keep it visible
yticks <- 10^seq(log10(ylim[1L]),log10(ylim[2L]));
yticks.minor <- rep(yticks[-length(yticks)],each=9L)*1:9;
plot(NA,xlim=xlim,ylim=ylim,xaxs='i',yaxs='i',axes=F,xlab='size (rows)',ylab='time (ms)',log='xy');
abline(v=xticks,col='lightgrey');
abline(h=yticks.minor,col='lightgrey',lty=3L);
abline(h=yticks,col='lightgrey');
axis(1L,xticks,parse(text=sprintf('10^%d',as.integer(log10(xticks)))));
axis(2L,yticks,parse(text=sprintf('10^%d',as.integer(log10(yticks)))),las=1L);
axis(4L,ymajors$y,ymajors$label,las=1L,tick=F,cex.axis=0.7,hadj=0.5);
for (joinType in rev(joinTypes)) { ## reverse to draw full first, since it's larger and would be more obtrusive if drawn last
x2 <- x1[x1$joinType==joinType,];
for (solType in solTypes) {
if (any(!is.na(x2[[solType]]))) {
xy <- spline(x2$size,x2[[solType]],xout=10^(seq(log10(x2$size[1L]),log10(x2$size[nrow(x2)]),len=NP)));
points(xy$x,xy$y,pch=pchs[[joinType]],col=cols[solType],cex=cexs[joinType],xpd=NA);
}; ## end if
}; ## end for
}; ## end for
## custom legend
## due to logarithmic skew, must do all distance calcs in inches, and convert to user coords afterward
## the bottom-left corner of the legend will be defined in normalized figure coords, although we can convert to inches immediately
leg.cex <- 0.7;
leg.x.in <- grconvertX(0.275,'nfc','in');
leg.y.in <- grconvertY(0.6,'nfc','in');
leg.x.user <- grconvertX(leg.x.in,'in');
leg.y.user <- grconvertY(leg.y.in,'in');
leg.outpad.w.in <- 0.1;
leg.outpad.h.in <- 0.1;
leg.midpad.w.in <- 0.1;
leg.midpad.h.in <- 0.1;
leg.sol.w.in <- max(strwidth(solTypes,'in',leg.cex));
leg.sol.h.in <- max(strheight(solTypes,'in',leg.cex))*1.5; ## multiplication factor for greater line height
leg.join.w.in <- max(strheight(joinTypes,'in',leg.cex))*1.5; ## ditto
leg.join.h.in <- max(strwidth(joinTypes,'in',leg.cex));
leg.main.w.in <- leg.join.w.in*length(joinTypes);
leg.main.h.in <- leg.sol.h.in*length(solTypes);
leg.x2.user <- grconvertX(leg.x.in+leg.outpad.w.in*2+leg.main.w.in+leg.midpad.w.in+leg.sol.w.in,'in');
leg.y2.user <- grconvertY(leg.y.in+leg.outpad.h.in*2+leg.main.h.in+leg.midpad.h.in+leg.join.h.in,'in');
leg.cols.x.user <- grconvertX(leg.x.in+leg.outpad.w.in+leg.join.w.in*(0.5+seq(0L,length(joinTypes)-1L)),'in');
leg.lines.y.user <- grconvertY(leg.y.in+leg.outpad.h.in+leg.main.h.in-leg.sol.h.in*(0.5+seq(0L,length(solTypes)-1L)),'in');
leg.sol.x.user <- grconvertX(leg.x.in+leg.outpad.w.in+leg.main.w.in+leg.midpad.w.in,'in');
leg.join.y.user <- grconvertY(leg.y.in+leg.outpad.h.in+leg.main.h.in+leg.midpad.h.in,'in');
rect(leg.x.user,leg.y.user,leg.x2.user,leg.y2.user,col='white');
text(leg.sol.x.user,leg.lines.y.user,solTypes[ord],cex=leg.cex,pos=4L,offset=0);
text(leg.cols.x.user,leg.join.y.user,joinTypes,cex=leg.cex,pos=4L,offset=0,srt=90); ## srt rotation applies *after* pos/offset positioning
for (i in seq_along(joinTypes)) {
joinType <- joinTypes[i];
points(rep(leg.cols.x.user[i],length(solTypes)),ifelse(colSums(!is.na(x1[x1$joinType==joinType,solTypes[ord]]))==0L,NA,leg.lines.y.user),pch=pchs[[joinType]],col=cols[solTypes[ord]]);
}; ## end for
title(titleFunc(overlap));
readline(sprintf('overlap %.02f',overlap));
}; ## end for
}; ## end plotRes()
titleFunc <- function(overlap) sprintf('R merge solutions: single-column integer key, 0..1:0..1 cardinality, %d%% overlap',as.integer(overlap*100));
plotRes(res,titleFunc,T);
Here's a second large-scale benchmark that's more heavy-duty, with respect to the number and types of key columns, as well as cardinality. For this benchmark I use three key columns: one character, one integer, and one logical, with no restrictions on cardinality (that is, 0..*:0..*
). (In general it's not advisable to define key columns with double or complex values due to floating-point comparison complications, and basically no one ever uses the raw type, much less for key columns, so I haven't included those types in the key columns. Also, for information's sake, I initially tried to use four key columns by including a POSIXct key column, but the POSIXct type didn't play well with the sqldf.indexed
solution for some reason, possibly due to floating-point comparison anomalies, so I removed it.)
makeArgSpecs.assortedKey.optionalManyToMany <- function(size,overlap,uniquePct=75) {
## number of unique keys in df1
u1Size <- as.integer(size*uniquePct/100);
## (roughly) divide u1Size into bases, so we can use expand.grid() to produce the required number of unique key values with repetitions within individual key columns
## use ceiling() to ensure we cover u1Size; will truncate afterward
u1SizePerKeyColumn <- as.integer(ceiling(u1Size^(1/3)));
## generate the unique key values for df1
keys1 <- expand.grid(stringsAsFactors=F,
idCharacter=replicate(u1SizePerKeyColumn,paste(collapse='',sample(letters,sample(4:12,1L),T))),
idInteger=sample(u1SizePerKeyColumn),
idLogical=sample(c(F,T),u1SizePerKeyColumn,T)
##idPOSIXct=as.POSIXct('2016-01-01 00:00:00','UTC')+sample(u1SizePerKeyColumn)
)[seq_len(u1Size),];
## rbind some repetitions of the unique keys; this will prepare one side of the many-to-many relationship
## also scramble the order afterward
keys1 <- rbind(keys1,keys1[sample(nrow(keys1),size-u1Size,T),])[sample(size),];
## common and unilateral key counts
com <- as.integer(size*overlap);
uni <- size-com;
## generate some unilateral keys for df2 by synthesizing outside of the idInteger range of df1
keys2 <- data.frame(stringsAsFactors=F,
idCharacter=replicate(uni,paste(collapse='',sample(letters,sample(4:12,1L),T))),
idInteger=u1SizePerKeyColumn+sample(uni),
idLogical=sample(c(F,T),uni,T)
##idPOSIXct=as.POSIXct('2016-01-01 00:00:00','UTC')+u1SizePerKeyColumn+sample(uni)
);
## rbind random keys from df1; this will complete the many-to-many relationship
## also scramble the order afterward
keys2 <- rbind(keys2,keys1[sample(nrow(keys1),com,T),])[sample(size),];
##keyNames <- c('idCharacter','idInteger','idLogical','idPOSIXct');
keyNames <- c('idCharacter','idInteger','idLogical');
## note: was going to use raw and complex type for two of the non-key columns, but data.table doesn't seem to fully support them
argSpecs <- list(
default=list(copySpec=1:2,args=list(
df1 <- cbind(stringsAsFactors=F,keys1,y1=sample(c(F,T),size,T),y2=sample(size),y3=rnorm(size),y4=replicate(size,paste(collapse='',sample(letters,sample(4:12,1L),T)))),
df2 <- cbind(stringsAsFactors=F,keys2,y5=sample(c(F,T),size,T),y6=sample(size),y7=rnorm(size),y8=replicate(size,paste(collapse='',sample(letters,sample(4:12,1L),T)))),
keyNames
)),
data.table.unkeyed=list(copySpec=1:2,args=list(
as.data.table(df1),
as.data.table(df2),
keyNames
)),
data.table.keyed=list(copySpec=1:2,args=list(
setkeyv(as.data.table(df1),keyNames),
setkeyv(as.data.table(df2),keyNames)
))
);
## prepare sqldf
initSqldf();
sqldf(paste0('create index df1_key on df1(',paste(collapse=',',keyNames),');')); ## upload and create an sqlite index on df1
sqldf(paste0('create index df2_key on df2(',paste(collapse=',',keyNames),');')); ## upload and create an sqlite index on df2
argSpecs;
}; ## end makeArgSpecs.assortedKey.optionalManyToMany()
sizes <- c(1e1L,1e3L,1e5L); ## 1e5L instead of 1e6L to respect more heavy-duty inputs
overlaps <- c(0.99,0.5,0.01);
solTypes <- setdiff(getSolTypes(),'in.place');
system.time({ res <- testGrid(makeArgSpecs.assortedKey.optionalManyToMany,sizes,overlaps,solTypes); });
## user system elapsed
## 38895.50 784.19 39745.53
The resulting plots, using the same plotting code given above:
titleFunc <- function(overlap) sprintf('R merge solutions: character/integer/logical key, 0..*:0..* cardinality, %d%% overlap',as.integer(overlap*100));
plotRes(res,titleFunc,F);
merge
समारोह और इसके वैकल्पिक पैरामीटर का उपयोग करके:
आंतरिक शामिल हों: merge(df1, df2)
इन उदाहरणों के लिए काम करेगा क्योंकि आर स्वचालित रूप से सामान्य चर नामों से फ्रेम में शामिल हो जाता है, लेकिन आप यह सुनिश्चित करने के लिए merge(df1, df2, by = "CustomerId")
निर्दिष्ट करना चाहते हैं कि आप केवल वांछित फ़ील्ड पर मेल खाते थे जो आप चाहते थे। यदि आप अलग-अलग डेटा फ्रेम में मिलान करने वाले चर के अलग-अलग नाम हैं तो आप by.x
और by.y
पैरामीटर का भी उपयोग कर सकते हैं।
बाहरी शामिल हों: merge(x = df1, y = df2, by = "CustomerId", all = TRUE)
बाएं बाहरी: merge(x = df1, y = df2, by = "CustomerId", all.x = TRUE)
दायां बाहरी: merge(x = df1, y = df2, by = "CustomerId", all.y = TRUE)
क्रॉस शामिल हों: merge(x = df1, y = df2, by = NULL)
जैसे ही आंतरिक जुड़ने के साथ, आप शायद मिलान करने वाले चर के रूप में आर को "ग्राहक आईडी" को स्पष्ट रूप से पास करना चाहते हैं। मुझे लगता है कि उन पहचानकर्ताओं को स्पष्ट रूप से बताने के लिए लगभग हमेशा सर्वोत्तम होता है जिन पर आप विलय करना चाहते हैं; यह सुरक्षित है अगर इनपुट डेटा.फ्रेम अप्रत्याशित रूप से बदलते हैं और बाद में पढ़ने में आसान होते हैं।