← Back to R Course | Chapter 2: Variables & Types | Lesson 7 of 7

Type की Checking और Conversion

Type checking पूछता है एक value किस तरह की चीज़ है, यह पूछने की तरह कि यह एक fruit है या vegetable। Conversion इसे किसी दूसरी तरह की में बदल देता है, जैसे text 5 को number 5 में बदलना।
Syntax
r
class(x)
typeof(x)
as.numeric(x)
as.character(x)
is.numeric(x)

Type Checking और Conversion

class() और typeof() एक value का type report करते हैं, जबकि as.numeric(), as.character(), और as.logical() जैसे functions types के बीच convert करते हैं।

जो conversion sense नहीं बनाता, जैसे as.numeric("abc"), एक error की बजाय एक warning के साथ NA produce करता है। Types explicitly check करना अलग sources से data mix करते वक्त subtle bugs avoid करता है।

Warning: as.numeric() के साथ एक non-numeric string convert करना, जैसे as.numeric("abc"), script रोकने की बजाय एक warning के साथ NA return करता है।

उदाहरण: Type Checking and Conversion

markup
x <- "42"
converted <- as.numeric(x)

cat(class(x), "->", class(converted), "\n")
cat(converted + 8, "\n")

bad <- as.numeric("abc")
cat(is.na(bad), "\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. as.numeric("abc") इस्तेमाल करना और एक error की उम्मीद करना, जब यह एक warning के साथ NA देता है।
  2. एक factor की तरह stored column से as.numeric() के साथ numbers पढ़ना, जो factor codes देता है; as.numeric(as.character(x)) इस्तेमाल करें।
  3. सिर्फ class() पर depend करना, जब typeof() underlying storage दिखाता है जैसे double।
चैप्टर सारांश
  • Assignment operators values को variables में store करते हैं, और R में numeric, integer, character, logical, और complex types हैं।
  • आप एक value का type check कर सकते हैं और इसे किसी दूसरे type में convert कर सकते हैं।
  • Types समझना आपको यह predict करने में मदद करता है कि operations कैसे behave करते हैं।
🔒

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.