[R] on gsub (simple, but not to me!) sintax

Ottorino-Luca Pantani ottorino-luca.pantani at unifi.it
Mon Nov 16 14:21:22 CET 2009


Dear R users,
my problem today deals with my ignorance on regular expressions.
a matter I recently discovered.

Consider the following

foo <-
c("V_7_101110_V",  "V_7_101110_V",  "V_9_101110_V",  "V_9_101110_V",
"V_9_s101110_V",  "V_9_101110_V",  "V_9_101110_V",  "V_11_101110_V",
"V_11_101110_V", "V_11_101110_V", "V_11_101110_V", "V_11_101110_V",
"V_17_101110_V", "V_17_101110_V")

what I'm trying to obtain is to add a zero in front of numbers below 10,
as in

c("V_07_101110_V",  "V_07_101110_V",  "V_09_101110_V",  "V_09_101110_V",
"V_09_101110_V",  "V_09_101110_V",  "V_09_101110_V",  "V_11_101110_V",
"V_11_101110_V", "V_11_101110_V", "V_11_101110_V", "V_11_101110_V",
"V_17_101110_V", "V_17_101110_V")


I'm able to do this on the emacs buffer through query-replace-regexp

C-M-%
search for
V_\(.\)_
and substitute with
V_0\1_

but I completely ignore how to do it with gsub within R
and the help is quite complicate to understand
(at least to me, at this moment in time)

I can search the vector through
grep("V_._",  foo)

but I always get errors either on
gsub('V_\(.\)_', 'V_0\1_', foo)


or I get not what I'm looking for on
gsub('V_._', 'V_0._', foo)
gsub('V_._', 'V_0\1_', foo)

Thanks in advance
-- 
Ottorino-Luca Pantani, Università di Firenze
Dip. Scienza del Suolo e Nutrizione della Pianta
P.zle Cascine 28 50144 Firenze Italia
Ubuntu 8.04.3 LTS -- GNU Emacs 23.0.60.1 (x86_64-pc-linux-gnu, GTK+ 
Version 2.12.9)
ESS version 5.5 -- R 2.10.0




More information about the R-help mailing list