[Rd] strcapture enhancement
William Dunlap
wdunlap at tibco.com
Wed Sep 21 20:06:21 CEST 2016
The new strcapture function in R-devel is handy, capturing
the matches to the parenthesized subpatterns in a regular
expression in the columns of a data.frame, whose column
names and classes are given by the 'proto' argument. E.g.,
> p1 <- data.frame(Name="", Number=0)
> str(strcapture("([[:alpha:]]*) +([[:digit:]]*)", c("Three 3", "Twenty
20"), proto=p1))
'data.frame': 2 obs. of 2 variables:
$ Name : Factor w/ 2 levels "Three","Twenty": 1 2
$ Number: num 3 20
I think it would be even nicer if it constructed its data.frame
using the check.names=FALSE and stringsAsFactors=FALSE
arguments. Then the names and types specified in the proto
argument would be respected instead of changing them as
in the following example
> p2 <- data.frame("The Name"="", "The Number"=0, stringsAsFactors=FALSE,
check.names=FALSE)
> str(strcapture("([[:alpha:]]*) +([[:digit:]]*)", c("Three 3", "Twenty
20"), proto=p2))
'data.frame': 2 obs. of 2 variables:
$ The.Name : Factor w/ 2 levels "Three","Twenty": 1 2
$ The.Number: num 3 20
Bill Dunlap
TIBCO Software
wdunlap tibco.com
[[alternative HTML version deleted]]
More information about the R-devel
mailing list