[R] help with regexp mass substitution

jim holtman jholtman at gmail.com
Fri Oct 2 14:12:10 CEST 2009


You need perl=TRUE:

gsub("^col(\\d{1,2}).(\\d{1,2}).(\\d{1,2})", "dom\\3.rig\\2.col\\1",
varnames, perl=TRUE)
  [1] "id.quest"        "txt.1.3"         "dom3.rig1.col1"
"dom3.rig1.col2"  "dom3.rig1.col3"  "dom3.rig1.col4"  "dom3.rig1.col5"
  [8] "txt.2.3"         "dom3.rig2.col1"  "dom3.rig2.col2"
"dom3.rig2.col3"  "dom3.rig2.col4"  "dom3.rig2.col5"  "txt.3.3"
 [15] "dom3.rig3.col1"  "dom3.rig3.col2"  "dom3.rig3.col3"
"dom3.rig3.col4"  "dom3.rig3.col5"  "txt.4.3"         "dom3.rig4.col1"
 [22] "dom3.rig4.col2"  "dom3.rig4.col3"  "dom3.rig4.col4"
"dom3.rig4.col5"  "txt.5.3"         "dom3.rig5.col1"  "dom3.rig5.col2"
 [29] "dom3.rig5.col3"  "dom3.rig5.col4"  "dom3.rig5.col5"  "txt.6.3"
       "dom3.rig6.col1"  "dom3.rig6.col2"  "dom3.rig6.col3"
 [36] "dom3.rig6.col4"  "dom3.rig6.col5"  "txt.7.3"
"dom3.rig7.col1"  "dom3.rig7.col2"  "dom3.rig7.col3"  "dom3.rig7.col4"
 [43] "dom3.rig7.col5"  "txt.8.3"         "dom3.rig8.col1"
"dom3.rig8.col2"  "dom3.rig8.col3"  "dom3.rig8.col4"  "dom3.rig8.col5"
 [50] "txt.9.3"         "dom3.rig9.col1"  "dom3.rig9.col2"
"dom3.rig9.col3"  "dom3.rig9.col4"  "dom3.rig9.col5"  "txt.10.3"
 [57] "dom3.rig10.col1" "dom3.rig10.col2" "dom3.rig10.col3"
"dom3.rig10.col4" "dom3.rig10.col5" "txt.11.3"
"dom3.rig11.col1"
 [64] "dom3.rig11.col2" "dom3.rig11.col3" "dom3.rig11.col4"
"dom3.rig11.col5" "txt.12.3"        "dom3.rig12.col1"
"dom3.rig12.col2"
 [71] "dom3.rig12.col3" "dom3.rig12.col4" "dom3.rig12.col5" "txt.13.3"
       "dom3.rig13.col1" "dom3.rig13.col2" "dom3.rig13.col3"
 [78] "dom3.rig13.col4" "dom3.rig13.col5" "txt.14.3"
"dom3.rig14.col1" "dom3.rig14.col2" "dom3.rig14.col3"
"dom3.rig14.col4"
 [85] "dom3.rig14.col5" "txt.15.3"        "dom3.rig15.col1"
"dom3.rig15.col2" "dom3.rig15.col3" "dom3.rig15.col4"
"dom3.rig15.col5"
 [92] "txt.16.3"        "dom3.rig16.col1" "dom3.rig16.col2"
"dom3.rig16.col3" "dom3.rig16.col4" "dom3.rig16.col5" "txt.17.3"
 [99] "dom3.rig17.col1" "dom3.rig17.col2"
>


On Fri, Oct 2, 2009 at 5:39 AM, Luca Braglia <braglia at poleis.eu> wrote:
> Hello *
>
> i have to rename a lot of variables, and, given that they have regular name constructs, I would like to use regexps.
>
> Here's a dump of my head(names(df))
>
> varnames <- c("id.quest", "txt.1.3", "col1.1.3", "col2.1.3", "col3.1.3",
> "col4.1.3", "col5.1.3", "txt.2.3", "col1.2.3", "col2.2.3", "col3.2.3",
> "col4.2.3", "col5.2.3", "txt.3.3", "col1.3.3", "col2.3.3", "col3.3.3",
> "col4.3.3", "col5.3.3", "txt.4.3", "col1.4.3", "col2.4.3", "col3.4.3",
> "col4.4.3", "col5.4.3", "txt.5.3", "col1.5.3", "col2.5.3", "col3.5.3",
> "col4.5.3", "col5.5.3", "txt.6.3", "col1.6.3", "col2.6.3", "col3.6.3",
> "col4.6.3", "col5.6.3", "txt.7.3", "col1.7.3", "col2.7.3", "col3.7.3",
> "col4.7.3", "col5.7.3", "txt.8.3", "col1.8.3", "col2.8.3", "col3.8.3",
> "col4.8.3", "col5.8.3", "txt.9.3", "col1.9.3", "col2.9.3", "col3.9.3",
> "col4.9.3", "col5.9.3", "txt.10.3", "col1.10.3", "col2.10.3",
> "col3.10.3", "col4.10.3", "col5.10.3", "txt.11.3", "col1.11.3",
> "col2.11.3", "col3.11.3", "col4.11.3", "col5.11.3", "txt.12.3",
> "col1.12.3", "col2.12.3", "col3.12.3", "col4.12.3", "col5.12.3",
> "txt.13.3", "col1.13.3", "col2.13.3", "col3.13.3", "col4.13.3",
> "col5.13.3", "txt.14.3", "col1.14.3", "col2.14.3", "col3.14.3",
> "col4.14.3", "col5.14.3", "txt.15.3", "col1.15.3", "col2.15.3",
> "col3.15.3", "col4.15.3", "col5.15.3", "txt.16.3", "col1.16.3",
> "col2.16.3", "col3.16.3", "col4.16.3", "col5.16.3", "txt.17.3",
> "col1.17.3", "col2.17.3")
>
> I'd like to convert only ^col variables to have eg
>
> >From "col4.1.3" --> To "dom3.rig1.col4"
>
> to test the matching regex I've tryied
>
> grep("^col\\d{1,2}.\\d{1,2}.\\d{1,2}$",varnames, perl=T, value=T)
>
> to substitute the ^col variables, I've tryied
>
> gsub("^col(\\d{1,2}).(\\d{1,2}).(\\d{1,2})", "dom\\3.rig\\2.col\\1", varnames)
>
> I'm a newbie with regexps and I got the starting vector (==varnames)
>
> all(gsub("^col(\\d{1,2}).(\\d{1,2}).(\\d{1,2})", "dom\\3.rig\\2.col\\1", varnames) == varnames )
>
> How can I do?
>
> Many thanks and have a nice weekend.
>
> Luca
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list