strsplit का उपयोग
strsplit text के एक piece को जहाँ भी एक separator appear हो वहाँ parts में काटता है, हर line पर एक sandwich काटने की तरह। इस तरह एक sentence words की एक list बन सकता है।
In this page:
Syntax
parts <- strsplit(x, split)
parts[[1]]
strsplit
strsplit(x, split) जहाँ भी separator pattern occur हो एक string को pieces में split करता है, एक list return करते हुए (क्योंकि हर input string अलग संख्या में pieces में split हो सकती है)।
पहला result access करने के लिए list में index करना चाहिए, जैसे strsplit(x, ",")[[1]]। यह CSV rows जैसे delimited text parse करने का standard तरीका है।
Note:
strsplit() हमेशा एक list return करता है, एक single string input के लिए भी -- pieces का character vector पाने के लिए [[1]] इस्तेमाल करें।
उदाहरण: strsplit
csv_line <- "apple,banana,cherry"
parts <- strsplit(csv_line, ",")[[1]]
print(parts)
cat("Second item:", parts[2], "\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. #}
आम गलतियां
- result को एक vector की तरह treat करना और सीधे
parts[2]लिखना, जबstrsplitएक list return करता है और आपको पहले[[1]]चाहिए। "."या"|"जैसे एक special regex character पर split करना, जो unexpectedly match करता है;fixed = TRUEइस्तेमाल करें।- separator पर खत्म होने वाली एक string के अंत में एक empty piece की उम्मीद करना, जब एक trailing empty string drop हो जाती है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: