[R] gsub and regex to tidy comma-limited values
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sat Mar 14 16:41:55 CET 2009
with the example below, i think the following solves your problem:
text = ',,,apple,,orange,,,,,lemon,strawberry,,,,'
gsub('^,+|,+(?=,)|,+$', '', text, perl=TRUE)
vQ
Tom wrote:
> sub(",$","",gsub("^,*|,*$|(,)*", "\\1",
> ",,,apple,,orange,,,,,lemon,strawberry,,,,"))
>
> It may not be the best solution, but it was my first thought
>
> Tom
>
> ----- Original Message ----- From: "Daren Tan" <darentan76 at gmail.com>
> To: <r-help at r-project.org>
> Sent: Saturday, March 14, 2009 6:42 PM
> Subject: [R] gsub and regex to tidy comma-limited values
>
>
>> I am cleaning up comma-limited values, so that only one comma
>> separates each value. Using the example below, as much as I try with
>> regex, I can't remove the last comma. I hope to have a one-liner
>> solution, if possible.
>>
>> gsub("^,*|,*$|(,)*", "\\1", ",,,apple,,orange,,,,,lemon,strawberry,,,,")
>> [1] "apple,orange,lemon,strawberry,"
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.
--
-------------------------------------------------------------------------------
Wacek Kusnierczyk, MD PhD
Email: waku at idi.ntnu.no
Phone: +47 73591875, +47 72574609
Department of Computer and Information Science (IDI)
Faculty of Information Technology, Mathematics and Electrical Engineering (IME)
Norwegian University of Science and Technology (NTNU)
Sem Saelands vei 7, 7491 Trondheim, Norway
Room itv303
Bioinformatics & Gene Regulation Group
Department of Cancer Research and Molecular Medicine (IKM)
Faculty of Medicine (DMF)
Norwegian University of Science and Technology (NTNU)
Laboratory Center, Erling Skjalgsons gt. 1, 7030 Trondheim, Norway
Room 231.05.060
More information about the R-help
mailing list