0

in my R space I have a couple of objects, which looks like

> objects(pattern="r_")
 [1] "r_1"  "r_10" "r_11" "r_12" "r_13" "r_14" "r_15" "r_16" "r_17" "r_18" "r_19" "r_2"  "r_20"
[14] "r_21" "r_22" "r_23" "r_24" "r_25" "r_26" "r_27" "r_28" "r_29" "r_3"  "r_30" "r_31" "r_32"
[27] "r_33" "r_34" "r_35" "r_36" "r_37" "r_38" "r_39" "r_4"  "r_40" "r_41" "r_42" "r_43" "r_44"
[40] "r_45" "r_46" "r_47" "r_48" "r_49" "r_5"  "r_50" "r_51" "r_52" "r_53" "r_54" "r_55" "r_56"
[53] "r_57" "r_58" "r_59" "r_6"  "r_60" "r_61" "r_62" "r_63" "r_64" "r_65" "r_66" "r_67" "r_68"
[66] "r_69" "r_7"  "r_70" "r_71" "r_72" "r_73" "r_74" "r_75" "r_76" "r_77" "r_78" "r_79" "r_8" 
[79] "r_80" "r_9"

these are all matrixes adn I want to rbind them, which can be done by

do.call("rbind", lapply(objects(pattern="r_"),get))->new

My problem is that the sequences of rbind is important. In the moment they are rbind in the sequence shown above r_1 then r_10...What I need would be to bind it numerically increasing so r_1,r_2,r_3....How can I do that?

heinheo
  • 557
  • 1
  • 4
  • 15
  • 2
    What you're looking for is actually how to sort a character vector in R. You can start here: http://stackoverflow.com/questions/15551914/is-it-possible-to-sort-a-vector-of-alphanumeric-values-using-lexical-ordering-in – Roman Luštrik May 08 '15 at 08:22
  • @Roman Luštrik i would be happy to install a package to do that kidn of sorting...is there maybe a function implemented which I coudl use to achieve lexical sorting? – heinheo May 08 '15 at 08:31
  • 2
    The package is `gtools` and the function `mixedsort`. See also the `mget` function that lets you get multiple objects without using `lapply`. – nicola May 08 '15 at 08:32
  • "If all of your data starts with `r_` then you could just strip that off and coerce to numeric and use in `order()`" - GregSnow – zx8754 May 08 '15 at 08:40

1 Answers1

0

You could try:

do.call("rbind", lapply(paste0("r_", seq_along(objects(pattern="r_"))), get))
J.R.
  • 3,838
  • 1
  • 21
  • 25