Re: [R] glob2rx function not working

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Tue 21 Feb 2006 - 05:19:09 EST

mtb954@gmail.com writes:

> Dear R users,
>
> Inspired by previous list discussion of the glob2rxc function, I am
> attempting to create a new vector called TOTAL by summing all vectors
> whose names begin with ABC:
>
> TOTAL = sum(list = ls(pattern = glob2rx("ABC*")))
>
> I'm running R 2.2.1 on Windows XP. Can anyone say what I'm missing?

Probably, but I'll save the sarcasm...

Consider this:

> ls()

[1] "as.alist.call" "f"             "fire"
[4] "Hgb"           "perulung"      "walk2"
[7] "x"             "y"             "zelazo"

> ls(pattern=glob2rx("f*"))
[1] "f" "fire"
> sum(list=ls(pattern=glob2rx("f*")))
Error in sum(..., na.rm = na.rm) : invalid 'mode' of argument

which is because

> sum(c("f","fire"))

Error in sum(..., na.rm = na.rm) : invalid 'mode' of argument

and has nothing to do with glob2rx, which is working fine.

You may be looking for constructions like

 do.call("sum",lapply(ls(pattern=glob2rx("ABC*")), get)))

or

 sum(unlist(lapply(ls(pattern=glob2rx("ABC*")), get)))

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Tue Feb 21 05:24:34 2006

This archive was generated by hypermail 2.1.8 : Tue 21 Feb 2006 - 14:38:23 EST