R में अनुपात की गणना कैसे करें (उदाहरण के साथ)


आप R में दो स्तंभों के मानों के बीच अनुपात की गणना करने के लिए निम्नलिखित विधियों का उपयोग कर सकते हैं:

विधि 1: बेस आर का उपयोग करें

 #calculate ratio between variable1 and variable2
df$ratio <- df$variable1/df$variable1

#calculate ratio between variable1 and variable2, rounded to 2 decimal places
df$ratio <- round(df$variable1/df$variable2, 2 )

विधि 2: dplyr का उपयोग करें

 library (dplyr)

#calculate ratio between variable1 and variable2
df <- df %>%
        mutate(ratio = variable1/variable2)

#calculate ratio between variable1 and variable2, rounded to 2 decimal places
df <- df %>%
        mutate(ratio = round(variable1/variable2, 2 ))

यह ट्यूटोरियल निम्नलिखित डेटा फ्रेम के साथ अभ्यास में प्रत्येक विधि का उपयोग करने का तरीका बताता है जो विभिन्न बास्केटबॉल खिलाड़ियों द्वारा लिए गए और प्रयास किए गए शॉट्स की कुल संख्या दिखाता है:

 #create data frame
df <- data. frame (players=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                 makes=c(4, 4, 3, 6, 7, 8, 3, 10),
                 attempts=c(12, 7, 5, 6, 10, 12, 5, 19))

#view data frame
df

  players makes attempts
1 to 4 12
2 B 4 7
3 C 3 5
4 D 6 6
5 E 7 10
6 F 8 12
7 G 3 5
8:10:19 a.m.

उदाहरण 1: आर आधार का उपयोग करके अनुपात की गणना करें

निम्नलिखित कोड दिखाता है कि आर आधार का उपयोग करके अंक और प्रयास कॉलम के मूल्यों के बीच अनुपात की गणना कैसे करें:

 #calculate ratio between makes and attempts columns
df$ratio <- df$makes/df$attempts

#view updated data frame
df

  players makes attempts ratio
1 A 4 12 0.3333333
2 B 4 7 0.5714286
3 C 3 5 0.6000000
4 D 6 6 1.0000000
5 E 7 10 0.7000000
6 F 8 12 0.6666667
7 G 3 5 0.6000000
8:10:19 AM 0.5263158

पहले खिलाड़ी के प्रयासों का अनुपात 4/12 = 0.33 है।

दूसरे शब्दों में, पहले खिलाड़ी ने अपने शॉट प्रयासों का लगभग 33% प्रयास किया।

हम अन्य सभी खिलाड़ियों के लिए अनुपात मानों की उसी तरह व्याख्या कर सकते हैं।

हम दशमलव स्थानों की एक निश्चित संख्या में अनुपात मानों को गोल करने के लिए राउंड() फ़ंक्शन का भी उपयोग कर सकते हैं:

 #calculate ratio between makes and attempts columns, rounded to 2 decimal places
df$ratio <- round(df$makes/df$attempts, 2 )

#view updated data frame
df

  players makes attempts ratio
1 to 4 12 0.33
2 B 4 7 0.57
3 C 3 5 0.60
4 D 6 6 1.00
5 E 7 10 0.70
6 F 8 12 0.67
7 G 3 5 0.60
8:10:19 0.53

अनुपात स्तंभ में प्रत्येक मान को अब दो दशमलव स्थानों तक पूर्णांकित किया गया है।

उदाहरण 2: dplyr का उपयोग करके अनुपात की गणना करें

निम्नलिखित कोड दिखाता है कि dplyr पैकेज का उपयोग करके अंक और प्रयास कॉलम में मानों के बीच अनुपात की गणना कैसे करें:

 library (dplyr)

#add new column that shows ratio of makes to attempts
df <- df %>%
        mutate(ratio = makes/attempts)

#view updated data frame
df

  players makes attempts ratio
1 A 4 12 0.3333333
2 B 4 7 0.5714286
3 C 3 5 0.6000000
4 D 6 6 1.0000000
5 E 7 10 0.7000000
6 F 8 12 0.6666667
7 G 3 5 0.6000000
8:10:19 AM 0.5263158

हम दशमलव स्थानों की एक निश्चित संख्या में अनुपात मानों को गोल करने के लिए राउंड() फ़ंक्शन का भी उपयोग कर सकते हैं:

 library (dplyr)

#add new column that shows ratio of makes to attempts, rounded to 2 decimal places
df <- df %>%
        mutate(ratio = round(makes/attempts, 2 ))

#view updated data frame
df

  players makes attempts ratio
1 to 4 12 0.33
2 B 4 7 0.57
3 C 3 5 0.60
4 D 6 6 1.00
5 E 7 10 0.70
6 F 8 12 0.67
7 G 3 5 0.60
8:10:19 0.53

अनुपात स्तंभ में प्रत्येक मान को अब दो दशमलव स्थानों तक पूर्णांकित किया गया है।

ध्यान दें कि आधार R विधि और dplyr विधि समान परिणाम देती हैं।

अतिरिक्त संसाधन

निम्नलिखित ट्यूटोरियल बताते हैं कि आर में अन्य सामान्य कार्य कैसे करें:

Dplyr का उपयोग करके अद्वितीय मानों को कैसे फ़िल्टर करें
Dplyr का उपयोग करके अनेक स्थितियों के आधार पर फ़िल्टर कैसे करें
आर में कॉलम में घटनाओं की संख्या की गणना कैसे करें

एक टिप्पणी जोड़ने

आपका ईमेल पता प्रकाशित नहीं किया जाएगा. आवश्यक फ़ील्ड चिह्नित हैं *