Dear all, I cannot figure out how to solve a small problem (well, not for me), surely somebody can help me in few seconds. I have a series of strings in a vector X of the type "xxx", "yyy", "zzz", "IgA", "IgG", "kkk", "IgM", "aaa". I want to substitute every ENTIRE string beginning with "Ig" with "0". So, I'd like to have "xxx", "yyy", "zzz", "0", "0", "kkk", "0", "aaa". I can easily identify these strings with grep("^Ig", X), but if I use this criterion in the sub() function (sub("^Ig", "0", X) I obviously get "0A", "0G" etc. I didn't expect to do it in this way and I tried with metacharacters and regexps in order to grep and substitute the whole word (\b \>, $). I don't post here my tryings, because they were obviously wrong. Please can you help me? Giulio _________________________________________________________________ Carica e scarica in un clic. Fino a 25 GB su SkyDrive [[alternative HTML version deleted]]
Here are two ways:> s <- c("xxx", "yyy", "zzz", "IgA", "IgG", "kkk", "IgM", "aaa") > > sub("^Ig.*", "0", s)[1] "xxx" "yyy" "zzz" "0" "0" "kkk" "0" "aaa"> > replace(s, grepl("^Ig", s), "0")[1] "xxx" "yyy" "zzz" "0" "0" "kkk" "0" "aaa" On Fri, Nov 13, 2009 at 10:13 AM, Giulio Di Giovanni <perimessaggini at hotmail.com> wrote:> > > > Dear all, > > > > I cannot figure out how to solve a small problem (well, not for me), surely somebody can help me in few seconds. > > > > I have a series of strings in a vector X of the type ?"xxx", "yyy", "zzz", "IgA", "IgG", "kkk", "IgM", "aaa". > > I want to substitute every ENTIRE string beginning with "Ig" with "0". > > So, I'd like to have "xxx", "yyy", "zzz", "0", "0", "kkk", "0", "aaa". > > > > I can easily identify these strings with grep("^Ig", X), but if I use this criterion in the sub() function (sub("^Ig", "0", X) I obviously get "0A", "0G" etc. > > > > I didn't expect to do it in this way and I tried with metacharacters and regexps in order to grep and substitute the whole word (\b \>, $). I don't post here my tryings, ?because they were obviously wrong. > > Please can you help me? > > > > Giulio > > _________________________________________________________________ > Carica e scarica in un clic. Fino a 25 GB su SkyDrive > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
On Nov 13, 2009, at 9:13 AM, Giulio Di Giovanni wrote:> > > > Dear all, > > > > I cannot figure out how to solve a small problem (well, not for me), > surely somebody can help me in few seconds. > > > > I have a series of strings in a vector X of the type "xxx", "yyy", > "zzz", "IgA", "IgG", "kkk", "IgM", "aaa". > > I want to substitute every ENTIRE string beginning with "Ig" with "0". > > So, I'd like to have "xxx", "yyy", "zzz", "0", "0", "kkk", "0", "aaa". > > > > I can easily identify these strings with grep("^Ig", X), but if I > use this criterion in the sub() function (sub("^Ig", "0", X) I > obviously get "0A", "0G" etc. > > > > I didn't expect to do it in this way and I tried with metacharacters > and regexps in order to grep and substitute the whole word (\b \>, > $). I don't post here my tryings, because they were obviously wrong. > > Please can you help me?x <- c("xxx", "yyy", "zzz", "IgA", "IgG", "kkk", "IgM", "aaa") > sub("^Ig.*$", "0", x) [1] "xxx" "yyy" "zzz" "0" "0" "kkk" "0" "aaa" You need to have the search regex include the entire element and not just the first couple of characters in order to replace the entire element. HTH, Marc Schwartz
Isn't this more straightforward? w <- grep("^Ig", vec) vec[w] <- "0" Regards, Adai Giulio Di Giovanni wrote:> > > Dear all, > > > > I cannot figure out how to solve a small problem (well, not for me), surely somebody can help me in few seconds. > > > > I have a series of strings in a vector X of the type "xxx", "yyy", "zzz", "IgA", "IgG", "kkk", "IgM", "aaa". > > I want to substitute every ENTIRE string beginning with "Ig" with "0". > > So, I'd like to have "xxx", "yyy", "zzz", "0", "0", "kkk", "0", "aaa". > > > > I can easily identify these strings with grep("^Ig", X), but if I use this criterion in the sub() function (sub("^Ig", "0", X) I obviously get "0A", "0G" etc. > > > > I didn't expect to do it in this way and I tried with metacharacters and regexps in order to grep and substitute the whole word (\b \>, $). I don't post here my tryings, because they were obviously wrong. > > Please can you help me? > > > > Giulio > > _________________________________________________________________ > Carica e scarica in un clic. Fino a 25 GB su SkyDrive > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Do you want to replace the whole word or the whole string? If the latter, use sub("^Ig.*", "0", x) (Matching is greedy, so .* matches are many characters as possible, here the rest of the string.) If 'whole word', we need a precise definition of word, but sub("^Ig\\w*", "0", x) is one conventional one (a word is alphanumeric: but you might not allow numbers). On Fri, 13 Nov 2009, Giulio Di Giovanni wrote:> Dear all, > > I cannot figure out how to solve a small problem (well, not for me), > surely somebody can help me in few seconds. > > I have a series of strings in a vector X of the type "xxx", "yyy", > "zzz", "IgA", "IgG", "kkk", "IgM", "aaa". > > I want to substitute every ENTIRE string beginning with "Ig" with "0". > > So, I'd like to have "xxx", "yyy", "zzz", "0", "0", "kkk", "0", "aaa". > > I can easily identify these strings with grep("^Ig", X), but if I > use this criterion in the sub() function (sub("^Ig", "0", X) I > obviously get "0A", "0G" etc. > > I didn't expect to do it in this way and I tried with metacharacters > and regexps in order to grep and substitute the whole word (\b \>, > $). I don't post here my tryings, because they were obviously wrong. > > Please can you help me? > > > > Giulio > > _________________________________________________________________ > Carica e scarica in un clic. Fino a 25 GB su SkyDrive > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hello All, I have downloaded and untarred R 2.10.0 onto my SUN UNIX machine. When I run ./configure, it runs through a series of questions and stops at this (and I get this when I try the make command): checking for iconv.h... yes checking for iconv... in libiconv checking whether iconv accepts "UTF-8", "latin1" and "UCS-*"... no configure: error: a suitable iconv is essential # make make: not found Could someone decipher this and tell me how I would get a suitable iconv? And is that indeed what it causing make to not be found? I did find this info below in the admin manual appendix. However, when I click on the link and run the suggested install, I get the same results. A suitably comprehensive iconv function is essential. The R usage requires iconv to be able to translate between "latin1" and "UTF-8", to recognize "" as the current encoding and to translate to and from the Unicode wide-character formats "UCS-[24][BL]E" - this is true for glibc but not of most commercial Unixes. However, you can make use of GNU libiconv (possibly as a plug-in replacement: see http://www.gnu.org/software/libiconv/.
On Fri, 13-Nov-2009 at 03:13PM +0000, Giulio Di Giovanni wrote: |> |> |> |> Dear all, |> |> |> |> I cannot figure out how to solve a small problem (well, not for me), surely somebody can help me in few seconds. |> |> |> |> I have a series of strings in a vector X of the type "xxx", "yyy", "zzz", "IgA", "IgG", "kkk", "IgM", "aaa". |> |> I want to substitute every ENTIRE string beginning with "Ig" with "0". |> |> So, I'd like to have "xxx", "yyy", "zzz", "0", "0", "kkk", "0", "aaa". |> |> |> |> I can easily identify these strings with grep("^Ig", X), but if I use this criterion in the sub() function (sub("^Ig", "0", X) I obviously get "0A", "0G" etc. |> |> |> |> I didn't expect to do it in this way and I tried with metacharacters and regexps in order to grep and substitute the whole word (\b \>, $). I don't post here my tryings, because they were obviously wrong. |> |> Please can you help me? I think this is what you need:> X <- c("xxx", "yyy", "zzz", "IgA", "IgG", "kkk", "IgM", "aaa") > X[grep("^Ig", X)] <- "0" > X[1] "xxx" "yyy" "zzz" "0" "0" "kkk" "0" "aaa">HTH -- ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. ___ Patrick Connolly {~._.~} Great minds discuss ideas _( Y )_ Average minds discuss events (:_~*~_:) Small minds discuss people (_)-(_) ..... Eleanor Roosevelt ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.