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

Rolf Turner rolf.turner at xtra.co.nz
Tue Apr 2 04:27:19 CEST 2013


On 04/02/2013 03:13 PM, Gundala Viswanath wrote:
> 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?

This seems to work:

  > x <- c("foo, foo2, foo3", "bar", "qux, qux1")
  > sub(",.*$","",x)
  [1] "foo" "bar" "qux"

     cheers,

         Rolf Turner



More information about the R-help mailing list