[R] How to remove all characters after comma in R

Peter Ehlers ehlers at ucalgary.ca
Tue Apr 2 04:29:14 CEST 2013


On 2013-04-01 19:23, arun wrote:
> gsub("\\,.*","",x)
> #[1] "foo" "bar" "qux"
> A.K.

No big deal, but does "," have to be escaped?

sub(",.*", "", x)

Peter Ehlers

>
> ----- Original Message -----
> From: Gundala Viswanath <gundalav at gmail.com>
> To: "r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch>
> Cc:
> Sent: Monday, April 1, 2013 10:13 PM
> Subject: [R] How to remove all characters after comma in R
>
> I have the following list of strings:
>
> x <- c("foo, foo2, foo3", "bar", "qux, qux1")
>
> what I want to do is to obtain
>
> foo, bar qux
>
> Namely for each element in the vector obtain only string
> before the first comma.
>
> What's the way to do it?
>
> - G.V.
>
>      [[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.
>
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list