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

Dry, Jonathan R Jonathan.Dry at astrazeneca.com
Fri Sep 25 16:01:46 CEST 2009


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.

--------------------------------------------------------------------------
AstraZeneca UK Limited is a company incorporated in Engl...{{dropped:21}}




More information about the R-help mailing list