[R] Character position command
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sun Aug 26 07:38:31 CEST 2007
On Sat, 25 Aug 2007, Mitchell Hoffman wrote:
> This is a very simple question, so I apologize I couldn't find it online:
>
> I want to shorten the string 'apples.pears' to 'apples'.
>
> string='apples.pears'
> string1=substr(string,0,x)
>
> For x above, I would like to have a command like charAt(string,"."), i.e.
> the position of the period in the word, but I can't seem to find a charAt
> command in R.
See ?regexpr
But a simpler solution is sub("\\..*", "", string).
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list