[R] Character position command

Gabor Grothendieck ggrothendieck at gmail.com
Sun Aug 26 03:46:05 CEST 2007


See ?regexpr to get the position; however, using sub we could remove
the dot and everything after it in one go.  See ?regexp and ?sub .
Also there are some links to info on regular expressions in the Links
box on this page:
http://gsubfn.googlecode.com

> n <- regexpr(".", "apples.pears", fixed = TRUE)
> substr("apples.pear", 1, n-1)
[1] "apples"


> sub("[.].*", "", "apples.pears")
[1] "apples"


On 8/25/07, Mitchell Hoffman <mh8675309 at gmail.com> 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.
>
> Thank you.
>
>        [[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.
>



More information about the R-help mailing list