Navarre Sabine
2005-Jun-22 14:42 UTC
[R] Is it possible to get the first letter of a word?
Hi, I would to get the first letter of a word like:> title_catTitleCat 1 Training I would like T from Training! Thnaks a lot for your help Sabine --------------------------------- T侀l侀chargez le ici ! [[alternative HTML version deleted]]
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 >substr(title_cat,1,1) Uwe Ligges> Sabine > > > --------------------------------- > > T??l??chargez le ici ! > [[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
Use substring() or substr(). Andy> From: Navarre Sabine > > 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 > > > --------------------------------- > > T??l??chargez le ici ! > [[alternative HTML version deleted]] > >
Dimitris Rizopoulos
2005-Jun-22 14:53 UTC
[R] Is it possible to get the first letter of a word?
you could use substr(), e.g., x <- c("asf", "dfds", "fdfdf dfd", "ehf rf ngfjvf", "gfg ") substr(x, 1, 1) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Navarre Sabine" <navarre_sabine at yahoo.fr> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, June 22, 2005 4:42 PM Subject: [R] Is it possible to get the first letter of a word?> 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 > > > --------------------------------- > > T??l??chargez le ici ! > [[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
?substring 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 > > > --------------------------------- > > T??l??chargez le ici ! > [[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-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
or What about;> strsplit("Training", split="")[[1]][1][1] "T" ____________________ Ken Knoblauch Inserm U371, Cerveau et Vision Department of Cognitive Neurosciences 18 avenue du Doyen Lepine 69500 Bron France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: 06 84 10 64 10 http://www.lyon.inserm.fr/371/
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 > > SabineThere 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