Is there a way in R to select certain characters from a line of text? I have some data that is presently in a large number of text files, and I would like to be able to select elements of each text file (elements are always on the same line, in the same position) and organize them into a table. Is there a tool to select text in this way in R? What I am looking for would be somewhat similar to the left() and right() functions in Excel. I have looked at the parse() and scan() functions, but don't think they can do what I want (although I could be wrong). Thank you, Tim [[alternative HTML version deleted]]
Maybe substring() is what you're looking for? Some examples:> substring("textstring",1,5)[1] "texts"> substring("textstring",3)[1] "xtstring"> substring("textstring",3,nchar("textstring"))[1] "xtstring" --- Tim Holland <timothyholland at gmail.com> wrote:> Is there a way in R to select certain characters from a line of text? I > have some data that is presently in a large number of text files, and I > would like to be able to select elements of each text file (elements are > always on the same line, in the same position) and organize them into a > table. Is there a tool to select text in this way in R? What I am looking > for would be somewhat similar to the left() and right() functions in Excel. > I have looked at the parse() and scan() functions, but don't think they can > do what I want (although I could be wrong). > Thank you, > Tim > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Tim Holland wrote:> > Is there a way in R to select certain characters from a line of text? I > have some data that is presently in a large number of text files, and I > would like to be able to select elements of each text file (elements are > always on the same line, in the same position) and organize them into a > table. Is there a tool to select text in this way in R? >Use substr() or substring() to select characters from a a text string, nchar() will give you its length, scan() can also help in reading data from text files, grep() can be used for search and selecting character strings in an array, having certain patterns. If your files are formatted in some way, consider read.table(), read.csv(), read.fwf() and friends. You should also read the "Data import/export" manual from the R documentation. Its pdf version was, probably, installed on your hard drive with R, and html is here: http://cran.r-project.org/doc/manuals/R-data.html pdf version is also here: http://cran.r-project.org/doc/manuals/R-data.pdf -- View this message in context: http://www.nabble.com/selecting-characters-from-a-line-of-text-tf3904063.html#a11074650 Sent from the R help mailing list archive at Nabble.com.