← Back to R Course | Chapter 8: Data Manipulation | Lesson 4 of 7

merge() का उपयोग

merge दो tables को एक column से जोड़ता है जो वे share करती हैं, students को इनके ID number से grades से match करने की तरह। यह related information को एक table में सीता है।

In this page:

  1. merge()
Syntax
r
merged <- merge(df1, df2, by = "key_column")

merge()

merge(df1, df2, by = "key") एक shared column के आधार पर दो data frames साथ जोड़ता है, एक SQL join जैसा।

Default से यह एक inner join perform करता है, दोनों frames में matching keys वाली सिर्फ rows रखते हुए; all = TRUE इसकी बजाय एक full outer join perform करता है। यह कई tables में फैले related data combine करने का standard तरीका है।

Note: एक left join के लिए all.x = TRUE set करें (पहली data frame से हर row रखें, matched हो या नहीं)।

उदाहरण: merge()

markup
people <- data.frame(id = c(1, 2, 3), name = c("Alice", "Bob", "Cara"))
scores <- data.frame(id = c(1, 2), score = c(95, 82))

merged <- merge(people, scores, by = "id")
print(merged)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. by.x और by.y के बिना हर data frame में अलग names वाले एक column पर merge करना, इसलिए R उन columns इस्तेमाल करने की कोशिश करता है जो match नहीं करते।
  2. यह उम्मीद करना कि unmatched rows रखी जाएँगी, जब default सिर्फ matches रखता है; बाकी रखने के लिए all = TRUE इस्तेमाल करें।
  3. duplicate keys वाले एक column पर merge करना, जो rows multiply कर देता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.