I am sorry, I'd like to split my column ("names") such that all
the
beginning of a string ("X..") is gone and only the rest of the text is
left.
x<-data.frame(names=c("X..aba","X..abb","X..abc","X..abd"))
x$names<-as.character(x$names)
(x)
str(x)
Can't figure out how to apply strsplit in this situation - without
using a loop. I hope it's possible to do it without a loop - is it?
Thanks a lot!
--
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com
Dimitri Liakhovitski wrote:> I am sorry, I'd like to split my column ("names") such that all the > beginning of a string ("X..") is gone and only the rest of the text is > left. > > x<-data.frame(names=c("X..aba","X..abb","X..abc","X..abd")) > x$names<-as.character(x$names) > (x) > str(x) > > Can't figure out how to apply strsplit in this situation - without > using a loop. I hope it's possible to do it without a loop - is it?If I understand, strsplit isn't necessary: sub("X\\.+", "", x$names)
Have a look at the stringr package It simplifies such things... ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Wed, Aug 4, 2010 at 8:42 PM, Dimitri Liakhovitski < dimitri.liakhovitski@gmail.com> wrote:> I am sorry, I'd like to split my column ("names") such that all the > beginning of a string ("X..") is gone and only the rest of the text is > left. > > x<-data.frame(names=c("X..aba","X..abb","X..abc","X..abd")) > x$names<-as.character(x$names) > (x) > str(x) > > Can't figure out how to apply strsplit in this situation - without > using a loop. I hope it's possible to do it without a loop - is it? > > Thanks a lot! > > > -- > Dimitri Liakhovitski > Ninah Consulting > www.ninah.com > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Try this:
gsub("X\\.\\.", "", x$names)
On Wed, Aug 4, 2010 at 2:42 PM, Dimitri Liakhovitski <
dimitri.liakhovitski@gmail.com> wrote:
> I am sorry, I'd like to split my column ("names") such that
all the
> beginning of a string ("X..") is gone and only the rest of the
text is
> left.
>
>
x<-data.frame(names=c("X..aba","X..abb","X..abc","X..abd"))
> x$names<-as.character(x$names)
> (x)
> str(x)
>
> Can't figure out how to apply strsplit in this situation - without
> using a loop. I hope it's possible to do it without a loop - is it?
>
> Thanks a lot!
>
>
> --
> Dimitri Liakhovitski
> Ninah Consulting
> www.ninah.com
>
> ______________________________________________
> R-help@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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
[[alternative HTML version deleted]]
Hi,
You already have great solutions. I just wanted to point out that
A) strsplit() works on the entire column automatically so you would
not need a loop
B) with the argument stringsAsFactors = FALSE, your character data
will not be converted to factor, so you would not need to convert it
back.
x <- data.frame(names =
c("X..aba","X..abb","X..abc","X..abd"),
stringsAsFactors = FALSE)
str(x)
Cheers,
Josh
On Wed, Aug 4, 2010 at 10:42 AM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:> I am sorry, I'd like to split my column ("names") such that
all the
> beginning of a string ("X..") is gone and only the rest of the
text is
> left.
>
>
x<-data.frame(names=c("X..aba","X..abb","X..abc","X..abd"))
> x$names<-as.character(x$names)
> (x)
> str(x)
>
> Can't figure out how to apply strsplit in this situation - without
> using a loop. I hope it's possible to do it without a loop - is it?
>
> Thanks a lot!
>
>
> --
> Dimitri Liakhovitski
> Ninah Consulting
> www.ninah.com
>
> ______________________________________________
> 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.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
On Aug 4, 2010, at 1:42 PM, Dimitri Liakhovitski wrote:> I am sorry, I'd like to split my column ("names") such that all the > beginning of a string ("X..") is gone and only the rest of the text is > left.I could not tell whether it was the string "X.." or the pattern "X.." that was your goal for matching and removal.> > x<-data.frame(names=c("X..aba","X..abb","X..abc","X..abd")) > x$names<-as.character(x$names)a) Instead of "names" which is heavily used function name, use something more specific. Otherwise you get: > names(x) "names" # and thereby avoid list comments about canines. b) Instead of coercing a character vector back to a character vector, use stringsAsFactors = FALSE. > x<-data.frame(nam1=c("X..aba","X..abb","X..abc","X..abd"), stringsAsFactors=FALSE) #Thus is the pattern version: > x$nam1 <- gsub("X..",'', x$nam1) > x nam1 1 aba 2 abb 3 abc 4 abd This is the string version: > x<-data.frame(nam1=c("X......aba","X.y.abb","X..abc","X..abd"), stringsAsFactors=FALSE) > x$nam1 <- gsub("X\\.+",'', x$nam1) > x nam1 1 aba 2 y.abb 3 abc 4 abd> (x) > str(x) > > Can't figure out how to apply strsplit in this situation - without > using a loop. I hope it's possible to do it without a loop - is it?-- David Winsemius, MD West Hartford, CT
Maybe I'm completely wrong, but I get sometimes names like X..123 when I import data through read.delim, read.table, ... When it's necessary I avoid the X..123 by adding read.delim(xxx, check.names=F) HTH Bart -- View this message in context: http://r.789695.n4.nabble.com/applying-strsplit-to-a-whole-column-tp2313915p2314619.html Sent from the R help mailing list archive at Nabble.com.