[R] strsplit (regex)

Gabor Grothendieck ggrothendieck at gmail.com
Wed Nov 12 07:11:50 CET 2008


You can do this one even more easily without regular
expressions like this:

substring(v, 1, 2)
substring(v, 3)

or with regexps:

sub("(..).*", "\\1", v)
sub("..(.*)", "\\1", v)

or in one line using strapply in the gsubfn package:

library(gsubfn) # see http://gsubfn.googlecode.com

out <- strapply(as.character(v), "(..)(.*)", c, simplify = rbind)


On Tue, Nov 11, 2008 at 2:33 PM, stephen sefick <ssefick at gmail.com> wrote:
> #how do I break these up into first two letters (RM), number, and then
> the last part
> #is there an easily accessible regex tutorial on the internet?
>
> v = (structure(1:122, .Label = c("RM215Temp", "RM215SpCond", "RM215DO.Conc",
> "RM215Depth", "RM215pH", "RM215ORP", "RM215Turbidity.", "RM215Battery",
> "RM215DO.", "SCTemp", "SCSpCond", "SCDO.Conc", "SCDepth", "SCpH",
> "SCORP", "SCTurbidity.", "SCBattery", "SCDO.", "RM202Temp", "RM202SpCond",
> "RM202DO.Conc", "RM202Depth", "RM202pH", "RM202ORP", "RM202Turbidity",
> "RM202Battery", "RM202DO.", "RM198Temp", "RM198SpCond", "RM198DO.Conc",
> "RM198Depth", "RM198pH", "RM198ORP", "RM198Turbidity.", "RM198Battery",
> "RM198DO.", "HCTemp", "HCSpCond", "HCDO.Conc", "HCDepth", "HCpH",
> "HCORP", "HCTurbidity.", "HCBattery", "HCDO.", "RM190Temp", "RM190SpCond",
> "RM190DO.Conc", "RM190Depth", "RM190pH", "RM190ORP", "RM190Turbidity.",
> "RM190Battery", "RM190DO.", "BCTemp", "BCSpCond", "BCDO.Conc",
> "BCDepth", "BCpH", "BCORP", "BCTurbidity.", "BCBattery", "BCDO.",
> "RM185Temp", "RM185SpCond", "RM185DO.Conc", "RM185Depth", "RM185pH",
> "RM185ORP", "RM185Turbidity.", "RM185Battery", "RM185DO.", "RM179Temp",
> "RM179SpCond", "RM179DO.Conc", "RM179Depth", "RM179pH", "RM179ORP",
> "RM179Turbidity.", "RM179Battery", "RM179DO.", "RM148Temp", "RM148SpCond",
> "RM148DO.Conc", "RM148Depth", "RM148pH", "RM148ORP", "RM148Turbidity.",
> "RM148Battery", "RM148DO.", "RM119Temp", "RM119SpCond", "RM119DO.Conc",
> "RM119Depth", "RM119pH", "RM119ORP", "RM119Turbidity.", "RM119Battery",
> "RM119DO.", "RM61Temp", "RM61SpCond", "RM61DO.Conc", "RM61Depth",
> "RM61pH", "RM61ORP", "RM61Turbidity.", "RM61Battery", "RM61DO.",
> "NSBL.Dflow", "RM148flow", "RM119flow", "RM61flow", "RM190heat",
> "RM185heat", "RM148heat", "RM119heat", "RM61heat", "RM190DOmass",
> "RM185DOmass", "RM148DOmass", "RM119DOmass", "RM61DOmass"), class = "factor"))
>
>
> thanks
>
> --
> Stephen Sefick
> Research Scientist
> Southeastern Natural Sciences Academy
>
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods.  We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
>                                                                -K. Mullis
>
> ______________________________________________
> 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