Hi all - this is a very basic question: how does one index substrings of a character string, for example get the 2nd through 5th characters of the string "testing", in the R language? Indexing with square brackets seems only to work for vectors, lists, etc., and although I've found plenty of functions for concatenating strings, running substitutions with regexps and the like, I seem to be at a loss when it comes to indexing or selecting substrings. I'm sure I'm missing something very obvious here, but I ask only after having scoured the basic help manuals. I am not so subscribed to this list, so if any replies could be sent directly to me. Thanks very much, -----Ben -- __________________________________________________________ Sign-up for your own personalized E-mail at Mail.com Search Smarter - get the new eXact Search Bar for free! http://www.exactsearchbar.com/
On Thu, 2003-11-06 at 19:53, Ben Day wrote:> Hi all - this is a very basic question: how does one index substrings > of a character string, for example get the 2nd through 5th characters > of the string "testing", in the R language? Indexing with square > brackets seems only to work for vectors, lists, etc., and although > I've found plenty of functions for concatenating strings, running > substitutions with regexps and the like, I seem to be at a loss when > it comes to indexing or selecting substrings. > > I'm sure I'm missing something very obvious here, but I ask only after > having scoured the basic help manuals. > > I am not so subscribed to this list, so if any replies could be sent > directly to me. > > Thanks very much, > -----Benunlist(strsplit("testing",""))[2:5] [1] "e" "s" "t" "i" Use strsplit() to convert the string into a list of characters, where "" is the separator. Then use unlist() to convert the list into a vector and index the 2:5 characters. See ?strsplit and ?unlist HTH, Marc Schwartz
Dear Ben and Mark, substring and substr are also helpful here. John At 08:10 PM 11/6/2003 -0600, Marc Schwartz wrote:>On Thu, 2003-11-06 at 19:53, Ben Day wrote: > > Hi all - this is a very basic question: how does one index substrings > > of a character string, for example get the 2nd through 5th characters > > of the string "testing", in the R language? Indexing with square > > brackets seems only to work for vectors, lists, etc., and although > > I've found plenty of functions for concatenating strings, running > > substitutions with regexps and the like, I seem to be at a loss when > > it comes to indexing or selecting substrings. > > > > I'm sure I'm missing something very obvious here, but I ask only after > > having scoured the basic help manuals. > > > > I am not so subscribed to this list, so if any replies could be sent > > directly to me. > > > > Thanks very much, > > -----Ben > > >unlist(strsplit("testing",""))[2:5] >[1] "e" "s" "t" "i" > >Use strsplit() to convert the string into a list of characters, where "" >is the separator. > >Then use unlist() to convert the list into a vector and index the 2:5 >characters. > >See ?strsplit and ?unlist > >HTH, > >Marc Schwartz > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox
On Thu, 2003-11-06 at 20:24, John Fox wrote:> Dear Ben and Mark, > > substring and substr are also helpful here. > > JohnQuite true. As usual, there is usually more than one way to do things. The above approach, given the C code, would be faster as the source vector and the offsets of the substring increase in size. This would become more influential if you want the resultant characters as a single substring as opposed to a vector of the characters, which my initial solution would yield.> unlist(strsplit("testing", ""))[2:5][1] "e" "s" "t" "i"> substr("testing", 2, 5)[1] "esti" Thanks John. Marc
Maybe Matching Threads
- splitting a character field in R
- Spliting columns, strings or reg exp returning substrings
- strsplit does not conform to documentation (PR#379)
- split a character variable into several character variable by a character
- how to subtract one string from another in R