Skip to content
Prev 319401 / 398506 Next

Extract letters from a column

HI,


tempdf<-read.table(text="
name,var1,var2,abb
Tom Cruiser,1,6,TomCru
Bread Pett,2,5,BrePet
Arnold Schwiezer,3,7,ArnSch 
",sep=",",header=TRUE,stringsAsFactors=FALSE)
?substr(tempdf$name, 4, 6) #as some of the firstnames differ in the number of characters
#[1] " Cr" "ad " "old"

?substr(gsub(".*\\s+","",tempdf$name),1,3)
#[1] "Cru" "Pet" "Sch"
A.K.



----- Original Message -----
From: Jorge I Velez <jorgeivanvelez at gmail.com>
To: SH <emptican at gmail.com>
Cc: r-help at r-project.org
Sent: Wednesday, March 13, 2013 10:10 AM
Subject: Re: [R] Extract letters from a column

Dear SH,

Hmmm... what about

substr(tempdf$name, 4, 6))

?

HTH,
Jorge.-
On Thu, Mar 14, 2013 at 1:06 AM, SH <emptican at gmail.com> wrote:

            
??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org 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.