Re: [R] Is it possible to get the first letter of a word?

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Thu 23 Jun 2005 - 00:55:35 EST

On Wed, 2005-06-22 at 16:42 +0200, Navarre Sabine wrote:
> Hi,
> I would to get the first letter of a word like:
>
> > title_cat
> TitleCat
> 1 Training
>
> I would like T from Training!
>
> Thnaks a lot for your help
>
> Sabine

There are multiple approaches, but you need to be careful, since it appears that your object is a factor. Thus you may need to convert to a character vector first:

> title_cat <- factor("Training")

> substr(as.character(title_cat), 1, 1)
[1] "T"

Otherwise:

> title_cat <- "Training"

> substr(title_cat, 1, 1)
[1] "T"

See ?substr for more information.

HTH, Marc Schwartz



R-help@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 Received on Thu Jun 23 01:17:16 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:57 EST