R में एकल कॉलम का नाम कैसे बदलें (उदाहरण के साथ)


आप R में डेटा फ़्रेम में किसी एकल कॉलम का नाम बदलने के लिए निम्न में से किसी भी विधि का उपयोग कर सकते हैं:

विधि 1: बेस आर का उपयोग करके एकल कॉलम का नाम बदलें

 #rename column by name
colnames(df)[colnames(df) == ' old_name '] <- ' new_name '

#rename column by position
#colnames(df)[ 2 ] <- ' new_name '

विधि 2: dplyr का उपयोग करके एकल कॉलम का नाम बदलें

 library (dplyr)

#rename column by name
df <- df %>% rename_at(' old_name ', ~' new_name ')

#rename column by position
df <- df %>% rename_at( 2 , ~' new_name ')

निम्नलिखित उदाहरण दिखाते हैं कि आर में निम्नलिखित डेटा फ्रेम के साथ व्यवहार में प्रत्येक विधि का उपयोग कैसे करें:

 #create data frame
df <- data. frame (team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1 A 99 33 30
2 B 90 28 28
3 C 86 31 24
4 D 88 39 24
5 E 95 34 28

उदाहरण 1: बेस आर का उपयोग करके एकल कॉलम का नाम बदलें

निम्नलिखित कोड दिखाता है कि कॉलम नामों का उपयोग करके पॉइंट कॉलम का नाम बदलकर total_points कैसे किया जाए:

 #rename 'points' column to 'total_points'
colnames(df)[colnames(df) == ' points '] <- ' total_points '

#view updated data frame
df

  team total_points assists rebounds
1 A 99 33 30
2 B 90 28 28
3 C 86 31 24
4 D 88 39 24
5 E 95 34 28

निम्नलिखित कोड दिखाता है कि कॉलम स्थिति का उपयोग करके पॉइंट कॉलम का नाम बदलकर total_points कैसे किया जाए:

 #rename column in position 2 to 'total_points'
colnames(df)[ 2 ] <- ' total_points '

#view updated data frame
df

  team total_points assists rebounds
1 A 99 33 30
2 B 90 28 28
3 C 86 31 24
4 D 88 39 24
5 E 95 34 28

ध्यान दें कि दोनों विधियाँ समान परिणाम देती हैं।

उदाहरण 2: dplyr का उपयोग करके एकल कॉलम का नाम बदलें

निम्नलिखित कोड दिखाता है कि dplyr में rename_at() फ़ंक्शन का उपयोग करके पॉइंट कॉलम का नाम बदलकर total_points कैसे किया जाए:

 library (dplyr)

#rename 'points' column to 'total_points' by name
df <- df %>% rename_at(' points ', ~' total_points ')

#view updated data frame
df

  team total_points assists rebounds
1 A 99 33 30
2 B 90 28 28
3 C 86 31 24
4 D 88 39 24
5 E 95 34 28

निम्नलिखित कोड दिखाता है कि dplyr में rename_at() फ़ंक्शन का उपयोग करके कॉलम स्थिति के आधार पर पॉइंट कॉलम का नाम बदलकर total_points कैसे किया जाए:

 library (dplyr)

#rename column in position 2 to 'total_points'
df <- df %>% rename_at( 2 , ~' total_points ')

#view updated data frame
df

  team total_points assists rebounds
1 A 99 33 30
2 B 90 28 28
3 C 86 31 24
4 D 88 39 24
5 E 95 34 28

ध्यान दें कि दोनों विधियाँ समान परिणाम देती हैं।

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

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

आर में विशिष्ट कॉलम का चयन कैसे करें
आर में कुछ कॉलम कैसे सुरक्षित रखें?
आर में एकाधिक कॉलम के आधार पर कैसे क्रमबद्ध करें

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

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