Skip to content
Prev 306436 / 398506 Next

Colsplit, removing parts of a string

Hi,
You can also try this:
df2 <- data.frame(df1, colsplit(df1$x, pattern = "_", names=c("str","name")))
df2list<-list(df2$str,df2$name)
df2[,2:3]<-sapply(df2list,function(x) gsub(".*(\\d)","\\1",x))
df2
?# ???????? x str name
#1 str1_name2?? 1??? 2
#2 str3_name5?? 3??? 5
A.K.



----- Original Message -----
From: Ista Zahn <istazahn at gmail.com>
To: Johannes Radinger <johannesradinger at gmail.com>
Cc: r-help at r-project.org
Sent: Thursday, September 27, 2012 7:43 AM
Subject: Re: [R] Colsplit, removing parts of a string

Hi Johannes,

On Thu, Sep 27, 2012 at 7:25 AM, Johannes Radinger
<johannesradinger at gmail.com> wrote:
You can remove them afterwords, e.g.,

df2$str <- gsub("[^0-9]", "", df2$str)
df2$name <- gsub("[^0-9]", "", df2$name)

Best,
Ista
______________________________________________
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.