[R] Spliting columns, strings or reg exp returning substrings

Ista Zahn istazahn at gmail.com
Sat Sep 26 16:15:10 CEST 2009


the colsplit function in the reshape package does this really easily.

--ista

> ---------- Forwarded message ----------
> From: "Dry, Jonathan R" <Jonathan.Dry at astrazeneca.com>
> To: <r-help at R-project.org>
> Date: Fri, 25 Sep 2009 15:01:46 +0100
> Subject: [R] Spliting columns, strings or reg exp returning substrings
> Currently as the first column in a data frame I have string values in the format xx_yy - I want to create a new column with just the substring xx (for each row in turn).  Three possible ways to do this might be (1) split the string by '_' using strsplit and paste the first of the resulting variables into a new column, but I have been unable to do this for each row of my data frame in turn (trying to use apply); (2) split the column into two based on '_', but I am not sure if this is possible; (3) use a regular expression to return the substring up to the '_', but I am unsure how to make a regular expression return the substring it matches to in R.
>
> Any ideas on all three counts would be gratefully recieved.




More information about the R-help mailing list