Type की Checking और Conversion
Type checking पूछता है एक value किस तरह की चीज़ है, यह पूछने की तरह कि यह एक fruit है या vegetable। Conversion इसे किसी दूसरी तरह की में बदल देता है, जैसे text 5 को number 5 में बदलना।
In this page:
Syntax
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
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")
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
as.numeric("abc")इस्तेमाल करना और एक error की उम्मीद करना, जब यह एक warning के साथNAदेता है।- एक factor की तरह stored column से
as.numeric()के साथ numbers पढ़ना, जो factor codes देता है;as.numeric(as.character(x))इस्तेमाल करें। - सिर्फ
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: