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

--ista

> ---------- Forwarded message ----------
> From: "Dry, Jonathan R" <jonathan....@astrazeneca.com>
> To: <r-help@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.

______________________________________________
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.

Reply via email to