- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract first two characters from a string in R?
A string can be short or long, also we can have a vector or list of strings as well in R. Extraction of partial string is common when we want to use the strings for single or multiple comparisons. If we want to extract first two characters from a string, we can use substr function and the syntax is substr(“String_object Or String”,start=1,stop=2)
Examples
x1<-"Tutorialspoint" substr(x1,start=1,stop=2) [1] "Tu" x2<-"Programing" substr(x2,start=1,stop=2) [1] "Pr" x3<-"R-Programming" substr(x3,start=1,stop=2) [1] "R-" x4<-"R<>Programming" substr(x4,start=1,stop=2) [1] "R<" x5<-"R!Programming" substr(x5,start=1,stop=2) [1] "R!" x6<-"R/Programming" substr(x6,start=1,stop=2) [1] "R/" x7<-"R@Programming" substr(x7,start=1,stop=2) [1] "R@" x8<-"R~Programming" substr(x8,start=1,stop=2) [1] "R~" x9<-"VALANY" substr(x9,start=1,stop=2) [1] "VA" x10<-"1 to 10" substr(x10,start=1,stop=2) [1] "1 " x11<-"10 to 20" substr(x11,start=1,stop=2) [1] "10" x12<-"North to South" substr(x12,start=1,stop=2) [1] "No" x13<-"No to Yes" substr(x13,start=1,stop=2) [1] "No" x14<-"1.5 and 2.5" substr(x14,start=1,stop=2) [1] "1." x15<-c("AB","CD","EF","GH","IJ") substr(x15,start=1,stop=2) [1] "AB" "CD" "EF" "GH" "IJ" x16<-c("ABCD","EFGH","IJKL") substr(x16,start=1,stop=2) [1] "AB" "EF" "IJ"
- Related Articles
- How to extract characters from a string in R?
- How to extract the first n characters from a string using Java?
- How to extract initial, last, or middle characters from a string in R?
- How to extract first value from a list in R?
- How to extract words from a string vector in R?
- How to extract only first sub-element from a list in R?
- How to extract string before slash from a vector in R?
- How to extract the last n characters from a string using Java?
- How to extract a string that lies between two strings in R?
- Extract only characters from given string in Python
- Extract string vector elements up to a fixed number of characters in R.
- How to extract number from string in R data frame?
- Python program to extract characters in given range from a string list
- How to extract first n values from each element in an R list?
- How to extract strings based on first character from a vector of strings in R?

Advertisements