- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 split a string vector that contain strings of equal sizes and have spaces between values then extract only few values in R?
A string vector can contain any value including spaces. Sometimes a string vector is read with some spaces as well and we want to split the vector then extract few values. For example, if a string has “ABC 123” then we might want to extract the number 123 so that we can use it in analysis. If the string vector has strings of equal sizes then it can be easily done with the help of substr function.
Examples
> x1<-c("1 00","1 01","1 02","1 03","1 03","1 04") > x1 [1] "1 00" "1 01" "1 02" "1 03" "1 03" "1 04" > Numeric_Last_two_x1<-substr(x1,start=4,stop=5) > Numeric_Last_two_x1 [1] "00" "01" "02" "03" "03" "04" > Numeric_Last_first_x1<-substr(x1,start=4,stop=4) > Numeric_Last_first_x1 [1] "0" "0" "0" "0" "0" "0" > Numeric_Last_last_x1<-substr(x1,start=5,stop=5) > Numeric_Last_last_x1 [1] "0" "1" "2" "3" "3" "4" > x2<-c("1 0","1 1","1 2","1 3","1 4","1 5") > x2 [1] "1 0" "1 1" "1 2" "1 3" "1 4" "1 5" > Last_value_x2<-substr(x2,start=4,stop=4) > Last_value_x2 [1] "0" "1" "2" "3" "4" "5" > First_value_x2<-substr(x2,start=1,stop=1) > First_value_x2 [1] "1" "1" "1" "1" "1" "1" > x3<-c("A 0","B 1","C 2","D 3","E 4","F 5") > x3 [1] "A 0" "B 1" "C 2" "D 3" "E 4" "F 5" > Last_value_x3<-substr(x3,start=4,stop=4) > Last_value_x3 [1] "0" "1" "2" "3" "4" "5" > x4<-c("A 1151","B 1152","C 1153","D 1154","E 1155","F 1156") > x4 [1] "A 1151" "B 1152" "C 1153" "D 1154" "E 1155" "F 1156" > Last_value_x4<-substr(x4,start=7,stop=7) > Last_value_x4 [1] "1" "2" "3" "4" "5" "6" > Last_three_value_x4<-substr(x4,start=5,stop=7) > Last_three_value_x4 [1] "151" "152" "153" "154" "155" "156" > Last_two_value_x4<-substr(x4,start=6,stop=7) > Last_two_value_x4 [1] "51" "52" "53" "54" "55" "56" > Last_numeric_value_x4<-substr(x4,start=4,stop=7) > Last_numeric_value_x4 [1] "1151" "1152" "1153" "1154" "1155" "1156" > x5<-c("System-1 191","System-2 352","System-3 498","System-4 154","System-5 327","System-6 456") > x5 [1] "System-1 191" "System-2 352" "System-3 498" "System-4 154" [5] "System-5 327" "System-6 456" > Last_numeric_value_x5<-substr(x5,start=11,stop=13) > Last_numeric_value_x5 [1] "191" "352" "498" "154" "327" "456" > Last_digit_x5<-substr(x5,start=13,stop=13) > Last_digit_x5 [1] "1" "2" "8" "4" "7" "6" > Last_two_digits_x5<-substr(x5,start=12,stop=13) > Last_two_digits_x5 [1] "91" "52" "98" "54" "27" "56" > Digits_in_System_x5<-substr(x5,start=8,stop=8) > Digits_in_System_x5 [1] "1" "2" "3" "4" "5" "6"
- Related Articles
- How to split string values that contain special characters in R?
- How to split a long string into a vector of substrings of equal sizes in R?
- How to extract all string values from a vector in R with maximum lengths?
- How to extract the names of vector values from a named vector in R?
- How to extract a string that lies between two strings in R?
- How to split comma separated values in an R vector?
- How to split a vector into smaller vectors of consecutive values in R?
- How to extract strings that contains a particular substring in an R vector?
- How to match the names of a vector in sequence with string vector values in another vector having same values in R?
- How to create a matrix using vector of string values in R?
- How to create a random vector of integers with increasing values only in R?
- How to split a vector by equal and different number of elements in R?
- How to extract strings based on first character from a vector of strings in R?
- Extract a data frame column values as a vector by matching a vector in R.
- How to replace values in a vector with values in the same vector in R?

Advertisements