← Back to R Course | Chapter 7: Data Structures | Lesson 6 of 7

which() का उपयोग

which आपको बताता है वे positions जहाँ एक condition true है, 'कौन-सी seats भरी हैं?' पूछने की तरह। यह आपको answers की बजाय seat numbers देता है।

In this page:

  1. which()
Syntax
r
which(condition)
vector_name[which(condition)]

which()

which(condition) *indices* return करता है जहाँ एक logical vector TRUE हो, values खुद नहीं।

यह तब useful है जब आपको matching elements की position जाननी हो, सिर्फ इन्हें filter नहीं करना। which.max() और which.min() सबसे बड़े/छोटे value का index ढूंढने के लिए shortcuts हैं।

Note: which.max(v), which(v == max(v))[1] का एक shortcut है, एक call में सबसे बड़ी value की position ढूंढते हुए।

उदाहरण: which()

markup
v <- c(3, 7, 2, 9, 4)

print(which(v > 4))
cat("Index of max value:", which.max(v), "\n")
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. यह उम्मीद करना कि which(v > 4) values return करेगा, जब यह इनकी positions return करता है; values के लिए v[v > 4] इस्तेमाल करें।
  2. which.max(v) इस्तेमाल करना और ties की सारी positions की उम्मीद करना, जब यह सिर्फ पहली return करता है।
  3. यह भूल जाना कि condition में NA values which() से ignore होती हैं, इसलिए वे silently drop हो जाती हैं।
🔒

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.