Skip to content
Back to formatted view

Raw Message

Message-ID: <1358963902.68687.YahooMailNeo@web142605.mail.bf1.yahoo.com>
Date: 2013-01-23T17:58:22Z
From: arun
Subject: extracting characters from a string
In-Reply-To: <1358962721.46908.YahooMailNeo@web172403.mail.ir2.yahoo.com>

Hi,
You could try this:
dat1<-read.table(text=pub,sep=",",fill=TRUE,stringsAsFactors=F)
dat2<- as.data.frame(do.call(cbind,lapply(dat1,function(x) gsub(" $","",gsub("^ |\\w+$","",x)))),stringsAsFactors=F)


?dat2
#??????? V1????????????? V2???????? V3???????? V4
#1?? Brown????????? Santos?????? Rome?? Don Juan 
#2 Benigni?????????????????????????????????????? 
#3? Arstra?? Van den Hoops?? lamarque?????? 
A.K.

----- Original Message -----
From: Biau David <djmbiau at yahoo.fr>
To: r help list <r-help at r-project.org>
Cc: 
Sent: Wednesday, January 23, 2013 12:38 PM
Subject: [R] extracting characters from a string

Dear All,

I have a data frame of vectors of publication names such as 'pub':

pub1 <- c('Brown DK, Santos R, Rome DF, Don Juan X')
pub2 <- c('Benigni D')
pub3 <- c('Arstra SD, Van den Hoops DD, lamarque D')

pub <- rbind(pub1, pub2, pub3)


I would like to construct a dataframe with only author's last name and each last name in columns and the publication in rows. Basically I want to get rid of the initials (max 2, always before a comma) and spaces surounding last name. I would like to avoid a loop.

ps: If I could have even a short explanation of the code that extract the values of the character string that would also be great!

?
David

??? [[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.